Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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_long don';当在结构选项中设置了标志时,t返回0_C_Getopt Long - Fatal编程技术网

C getopt_long don';当在结构选项中设置了标志时,t返回0

C getopt_long don';当在结构选项中设置了标志时,t返回0,c,getopt-long,C,Getopt Long,我在C中使用getopt_long时遇到了一个问题。如所述,结构如下: struct option{ const char *name; int has_arg; int *flag; int val; }; #include<stdio.h> #include<getopt.h> int help; int output; int verbose; const char *short

我在C中使用getopt_long时遇到了一个问题。如所述,结构如下:

struct option{ 
         const char *name; 
         int has_arg; 
         int *flag; 
         int val; 
};
#include<stdio.h>
#include<getopt.h>

int help;
int output;
int verbose;

const char *short_option = "ho:v";
const struct option long_options[] = {
    {"help", 0, &help, 'h'},
    {"output", 1, &output, 'o'},
    {"verbose", 0, &verbose, 'v'},
    {NULL, 0, NULL, 0}
};

void print_help()
{
    printf("in option help: \n");
}

void print_output(char *content)
{
    printf("in option output: argument is %s\n",content);
}

void print_verbose()
{
    printf("in option verbose\n");

}

void print_args()
{
    printf("help is %d\n",help);
    printf("output is %d\n",output);
    printf("verbose is %c\n",verbose);
}

int main(int argc, char **argv)
{
    int next_option;
    while( (next_option = getopt_long(argc, argv, short_option, long_options, NULL )) == 0)
    {
        printf("next_option is %c\n",next_option);
        print_args();
    }

    return 0;
}
如果设置了flag,getopt_long应该返回0,并将*flag设置为val。 但在我的代码中,getopt_long return val和*标志保持不变。代码如下:

struct option{ 
         const char *name; 
         int has_arg; 
         int *flag; 
         int val; 
};
#include<stdio.h>
#include<getopt.h>

int help;
int output;
int verbose;

const char *short_option = "ho:v";
const struct option long_options[] = {
    {"help", 0, &help, 'h'},
    {"output", 1, &output, 'o'},
    {"verbose", 0, &verbose, 'v'},
    {NULL, 0, NULL, 0}
};

void print_help()
{
    printf("in option help: \n");
}

void print_output(char *content)
{
    printf("in option output: argument is %s\n",content);
}

void print_verbose()
{
    printf("in option verbose\n");

}

void print_args()
{
    printf("help is %d\n",help);
    printf("output is %d\n",output);
    printf("verbose is %c\n",verbose);
}

int main(int argc, char **argv)
{
    int next_option;
    while( (next_option = getopt_long(argc, argv, short_option, long_options, NULL )) == 0)
    {
        printf("next_option is %c\n",next_option);
        print_args();
    }

    return 0;
}
#包括
#包括
智力帮助;
整数输出;
int冗长;
const char*short_option=“ho:v”;
const struct option long_options[]={
{“help”,0,&help,'h'},
{“output”,1,&output,'o'},
{“verbose”,0,&verbose,'v'},
{NULL,0,NULL,0}
};
void print_help()
{
printf(“在选项帮助中:\n”);
}
无效打印输出(字符*内容)
{
printf(“在选项输出中:参数为%s\n”,内容);
}
无效打印\u详细()
{
printf(“在选项详细\n中”);
}
无效打印参数()
{
printf(“帮助是%d\n”,帮助);
printf(“输出为%d\n”,输出);
printf(“详细是%c\n”,详细);
}
int main(int argc,字符**argv)
{
int next_选项;
while((next_option=getopt_long(argc,argv,short_option,long_options,NULL))==0)
{
printf(“下一个\u选项是%c\n”,下一个\u选项);
打印参数();
}
返回0;
}
有人可以帮忙吗?

如果您查看,您将看到:

getopt_long()getopt_long_only()在识别短选项时也返回选项字符。对于长选项,如果标志为NULL,则返回val,否则返回0


因此,如果在调用程序时使用short选项,它将返回该选项字符。要使函数按您所希望的方式运行,在调用程序时必须使用长参数。

如何运行程序?我想知道的是,你是从多头期权还是短头期权开始呢?我无法重现所描述的行为。如果使用长选项,这里的一切都很好。