gpt4 book ai didi

c - poptGetArgs 返回 null。

转载 作者:行者123 更新时间:2023-11-30 17:52:20 25 4
gpt4 key购买 nike

我正在使用 poptGetArgs 读取单个选项的多个值。但它总是给出 null 作为返回值。我在下面发布了我的代码。如果有任何错误,请帮我解决。

int main(int argc, char **argv)
{
char filename[ 128 ], symbol[32];
memset(filename, 0x0, 128);
memset(symbol, 0x0, 32);

struct poptOption opttable[] =
{
{ "file", 'f', POPT_ARG_STRING, filename, INPUT_NAME, "filenames to read", "list of files we need to read" },
{ "symbol", 'r', POPT_ARG_STRING, symbol, SYMBOL, "symbol to view", NULL },
{ NULL, 0, 0, NULL, 0 }
};
poptContext options_socket = poptGetContext( NULL, argc, ( const char **)argv, opttable, 0 );

int optionvalue(0);
while( optionvalue > -1 )
{
optionvalue = poptGetNextOpt( options_socket );
if(optionvalue == INPUT_NAME)
{
const char ** files = poptGetArgs( options_socket );
if( files == NULL )
{
printf("There was an error while reading input files\n");
}
}
else if( optionvalue == SYMBOL)
{
strcpy(symbol, poptGetOptArg( options_socket ));
printf("symbol you are giving as input is :%s, option value:%d\n", symbol, optionvalue);
}
}
return 0;
}

最佳答案

这是因为我试图读取中间剩余的参数(在文件选项读取之后)。但所有剩余选项只能在最后阅读。意味着我需要在 while( optionvalue > -1 ) 循环之后读取参数。我修改后的代码是

enum
{
INPUT_NAME=1,
SYMBOL
};
int main(int argc, char **argv)
{
char filename[ 128 ], symbol[32];
memset(filename, 0x0, 128);
memset(symbol, 0x0, 32);

struct poptOption opttable[] =
{
{ "file", 'f', POPT_ARG_STRING, filename, INPUT_NAME, "filenames to read", "list of files we need to read" },
{ "symbol", 'r', POPT_ARG_STRING, symbol, SYMBOL, "symbol to view", NULL },
{ NULL, 0, 0, NULL, 0 }
};
poptContext options_socket = poptGetContext( NULL, argc, ( const char **)argv, opttable, 0 );

int optionvalue(0);
while( optionvalue > -1 )
{
optionvalue = poptGetNextOpt( options_socket );
if(optionvalue == INPUT_NAME)
{
strcpy(filename, poptGetOptArg( options_socket));
printf("file name you are giving as input is :%s, option value:%d\n", filename, optionvalue);
// const char ** files = poptGetArgs( options_socket );
}
else if( optionvalue == SYMBOL)
{
strcpy(symbol, poptGetOptArg( options_socket ));
printf("symbol you are giving as input is :%s, option value:%d\n", symbol, optionvalue);
}
}
const char ** files = poptGetArgs( options_socket );
if(files == NULL)
{
printf("There are no other files left\n");
}
return 0;
}

现在工作正常。

关于c - poptGetArgs 返回 null。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16277548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com