Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:getopt():选项似乎无效_C_Getopt - Fatal编程技术网

C:getopt():选项似乎无效

C:getopt():选项似乎无效,c,getopt,C,Getopt,我正在尝试以下C代码(保存在名为testgetopt.C的文件中): 但无论是否包含-d,我总是将调试设置为0。程序应该将-d设置为1,否则设置为0 做错了什么 以下是一些例子: 让我们先用no-d试试 ./a.out -i in.dat -o out.dat debug = 0 让我们下一步尝试包含-d ./a.out -d -i in.dat -o out.dat debug = 0 提前感谢您的帮助和建议 问题是,您正在调用getopt作为一个健全性检查,然后在成功时不使用其结果。这意

我正在尝试以下C代码(保存在名为testgetopt.C的文件中):

但无论是否包含-d,我总是将调试设置为0。程序应该将-d设置为1,否则设置为0

做错了什么

以下是一些例子:

让我们先用no-d试试

./a.out -i in.dat -o out.dat
debug = 0
让我们下一步尝试包含-d

./a.out -d -i in.dat -o out.dat
debug = 0

提前感谢您的帮助和建议

问题是,您正在调用
getopt
作为一个健全性检查,然后在成功时不使用其结果。这意味着第一个选项总是被丢弃。您可以通过将
-d
作为非首选选项来轻松验证这一点:

$ ./a.out -i in.dat -o out.dat -d
debug = 1
input file = (null), output file = out.dat
您可以看到,
-d
这次确实生效,但是第一个参数
-i
没有设置

有几种方法可以解决这个问题。建议的方法(IMHO)是删除第一个
getopt
调用。然后更改健全性检查以验证实际选项。也就是说,在
getopt
循环之后应该有代码,用于验证是否提供了所有必需的选项

例如:

int main(int argc, char **argv) { 
    char c;
    size_t i;
    char *itext = NULL, *otext = NULL;
    FILE *fout;
    short debug = 0;

    /* REMOVED THIS BLOCK OF CODE
    if ((c = getopt(argc, argv, "di:o:")) == EOF) {
        usage(argv[0]);
        exit(1);
    }
    */

    while ((c = getopt(argc, argv, "di:o:")) != EOF) {
        switch (c) {
        case 'd':
            debug = 1;
            break;
        case 'i':
            itext = optarg;
            break;
        case 'o':
            otext = optarg;
            break;
        case '?':
            if (optopt == 'i' || optopt == 'o')
                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
            else if (isprint (optopt))
                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
            else
                fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
            usage(argv[0]);
            exit(1);
        default:
            fprintf(stderr, "Both -i and -o are needed\n");
            usage(argv[0]);
            exit(1);
        }
    }

    /* ADD SOME ARGUMENT SANITY CHECKS */
    if (!itext || !otext) {
        printf("Missing mandatory arguments\n");
        exit(1);
    }

    printf("debug = %i\n", debug);
    if (debug) 
        printf ("input file = %s, output file = %s\n", itext, otext);
    return EXIT_SUCCESS;
}

因为您要调用
getopt
两次,所以需要重置
optind=1在调用之间。

您是如何尝试调试的?在循环内部打印出
getopt
的结果将为您提供数据,您可以根据这些数据建立正在发生的事情的理论,尽管善良的堆栈溢出器已经为您慷慨地完成了这项工作。对不起,我不明白“然后更改健全性检查以验证实际选项。也就是说,在getopt循环之后应该有代码来验证是否提供了所有必需的选项。“你能提供一个参考/例子吗?谢谢我添加了一个示例代码,它删除了初始的
getopt
检查,并在命令行解析之后添加了一些健全性检查。健全性检查只是一个例子,您可能需要检查不同的/其他要求。顺便问一下,“默认”为什么不起作用?下面是我得到的:./a.out-I tmp缺少强制参数用法:./a.out[-d]-我输入TIFF文件-o输出原始文件注意:-d表示调试选项为什么它不会触发默认消息,即需要-I和-o?这很有效,谢谢!但我想知道如何不叫它两次!你是对的,真正的答案是不要调用
getopt()
两次@Kaylum的答案给出了一些很好的建议,我似乎不能同时检查两个答案!做一件事会拿走另一件的支票!我会留下这个评论,并检查其他,因为这是在我想更多的行。又来了!你只能接受一个答案。在这种情况下,@kaylum的更完整,所以我接受这个。
$ ./a.out -i in.dat -o out.dat -d
debug = 1
input file = (null), output file = out.dat
int main(int argc, char **argv) { 
    char c;
    size_t i;
    char *itext = NULL, *otext = NULL;
    FILE *fout;
    short debug = 0;

    /* REMOVED THIS BLOCK OF CODE
    if ((c = getopt(argc, argv, "di:o:")) == EOF) {
        usage(argv[0]);
        exit(1);
    }
    */

    while ((c = getopt(argc, argv, "di:o:")) != EOF) {
        switch (c) {
        case 'd':
            debug = 1;
            break;
        case 'i':
            itext = optarg;
            break;
        case 'o':
            otext = optarg;
            break;
        case '?':
            if (optopt == 'i' || optopt == 'o')
                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
            else if (isprint (optopt))
                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
            else
                fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
            usage(argv[0]);
            exit(1);
        default:
            fprintf(stderr, "Both -i and -o are needed\n");
            usage(argv[0]);
            exit(1);
        }
    }

    /* ADD SOME ARGUMENT SANITY CHECKS */
    if (!itext || !otext) {
        printf("Missing mandatory arguments\n");
        exit(1);
    }

    printf("debug = %i\n", debug);
    if (debug) 
        printf ("input file = %s, output file = %s\n", itext, otext);
    return EXIT_SUCCESS;
}