【问题标题】:C user input to array used as if statementC 用户对数组的输入用作 if 语句
【发布时间】:2016-07-01 15:58:29
【问题描述】:

我正在编写从命令行获取的代码。用户将在命令行中输入例如./a.out -l 2 4 6。这里的目标是遍历数组并查看是否出现'-l''-s'。如果出现 '-l',则 x = 1,如果 '-s' x = 2,如果 x = 0。现在引发的问题是第 7 行和第 12 行的指针和整数之间的比较。也是一个多字符字符常量在第 12 行,我不确定为什么在第 9 行正常时会抛出它。我将如何更改我的 if 语句以解决引发的问题?我的代码如下:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){

  int x;

  for(; *argv != '\0'; argv++){
    if(*argv == '-l'){
      x = 1;
  }
    else if(*argv == '-s'){
      x = 2; 
  } 
    else{
     x = 0;
  }
}
  printf("%d",x);
  return 0;
}

【问题讨论】:

  • 相同的答案略有不同的问题,我不是在比较两个字符串,而是从现在在数组中的字符串输入中寻找一个字符
  • 是的,您正在尝试比较两个字符串...如果不是,您为什么写*argv == '-s'

标签: c string-comparison command-line-arguments


【解决方案1】:

字符串用双引号指定,而不是用于字符的单引号。此外,您不能使用== 来比较字符串。为此使用strcmp

if(strcmp(*argv,"-l") == 0){

...

if(strcmp(*argv,"-s") == 0){

您的输出也不会如您所愿。每次检查下一个参数时都会覆盖x,因此结果将仅取决于最后一个。当满足这两个条件之一时,您需要将break 退出循环。

【讨论】:

  • 我的输出是 x = 0,当我做 ./a.out -l 2 4 6 任何想法时,这是不正确的?
【解决方案2】:

除了其他人指出的错误之外,您可能在某些时候想要进行正确的命令行解析。在 POSIX 系统上,这是通过在 unistd.h 标头中声明的 getopt() 库函数完成的(这不是“C 标准”C 代码)。

我已经通过一个选项 -x 扩展了您的要求,该选项接受一个整数参数,说明 x 应该设置为:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    int x = 0; /* default zero for x */
    int i, tmp;
    int opt;

    char *endptr;

    /* the ':' in the string means "the previous letter takes an argument" */
    while ((opt = getopt(argc, argv, "lsx:")) != -1) {
        switch (opt) {
        case 'l':
            x = 1;
            break;
        case 's':
            x = 2;
            break;
        case 'x':
            /* validate input, must be integer */
            tmp = (int)strtol(optarg, &endptr, 10);
            if (*optarg == '\0' || *endptr != '\0')
                printf("Illegal value for x: '%s'\n", optarg);
            else
                x = tmp;
            break;
        case '?': /* fallthrough */
        default:
            /* call routine that outputs usage info here */
            puts("Expected -s, -l or -x N");
        }
    }

    argc -= optind; /* adjust */
    argv += optind; /* adjust */

    printf("x = %d\n", x);

    puts("Other things on the command line:");
    for (i = 0; i < argc; ++i)
        printf("\t%s\n", argv[i]);

    return EXIT_SUCCESS;
}

运行它:

$ ./a.out -l
x = 1
Other things on the command line:

$ ./a.out -s
x = 2
Other things on the command line:

最后一个选项“获胜”:

$ ./a.out -s -x -78
x = -78
Other things on the command line:

这里,-s 是最后一个:

$ ./a.out -s -x -78 -ls
x = 2
Other things on the command line:

$ ./a.out -s -q
./a.out: illegal option -- q
Expected -s, -l or -x N
x = 2
Other things on the command line:

解析在第一个非选项处停止:

$ ./a.out -s "hello world" 8 9 -x 90
x = 2
Other things on the command line:
    hello world
    8
    9
    -x
    90

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2020-09-24
    • 2016-07-26
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多