我正在尝试在 optarg 上使用 atoi,但它也可能是任何东西。我一直在试图弄清楚为什么我的 getopt_long 不起作用。当我输入我的 switch 语句 optarg 设置为 null 并保持这种状态。我检查了我的冒号,它们是正确的。这是我的代码。
static struct option long_options[] =
{
{"algorithm", required_argument, 0, 'a'},
{"reverse", no_argument, 0, 'r'},
{"key", required_argument, 0, 'k'},
{"output", required_argument, 0, 'o'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{0, 0, 0, 0}
};
int option_index = 0;
int c;
//Getopt to get the correct options from the command line.
while ((c = getopt_long(argc, argv, "a:rk:o:hV", long_options,
&option_index)) != -1)
{
bool endOption = false;
if (endOption) break;
switch (c)
{
case 0:
{
endOption = true;
break;
}
case 'a':
{
if (optarg == "insertion") algorithm = 0;
break;
}
case 'r':
{
reverseFlag = true;
break;
}
case 'k':
{
while (optarg != " ")
{
if (optarg == ",")
{
optarg++;
}
else
{
sortOrder.push_back(atoi(optarg)); //error here
optarg++;
}
}
}
case 'o':
{
fileFlag = true;
break;
}
case 'h':
case 'V':
default:
{
cerr<<"You have entered an incorrect flag, do it better"<<endl;
break;
}
}
}
//更多东西//
我试过使用双冒号和其他所有东西。