我试图getopt_long()第一次使用该函数只是我遇到了不是标志的参数问题.例如,在我的代码中,当给出一个未知参数时,我想将它用作输入文件.当我只使用文件名运行它时,它不打印,如果我第一次使用标志,任何标志,那么我可以打印它.
我怎样才能解决这个问题?
#include <stdio.h>
#include <getopt.h>
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"input", required_argument, 0, 'i'},
{"output", required_argument, 0, 'o'},
{"algorithm", required_argument, 0, 'a'},
{0, 0, 0, 0}
};
int main(int argc, char *argv[]) {
int c;
int option_index = 0;
while(42) {
c = getopt_long(argc, argv, "hi:o:a:", long_options,
&option_index);
if(c == -1)
break;
switch(c) {
case 'h': /* --help */
printf("--help flag\n");
break;
case 'i': /* --input */
printf("--input flag\n");
break;
case 'o': …Run Code Online (Sandbox Code Playgroud)