c中的两个不同的命令行参数
two different command line argument in c
我想解析两个不同的命令行参数,这样:
file -f something -o something
我在互联网上找到了一些代码并对其进行了更改,但我无法解析它们。
int
main (int argc, char **argv)
{
char *fvalue = NULL;
char *ovalue = NULL;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "fo:")) != -1)
switch (c)
{
case 'f':
fvalue = optarg;
break;
case 'o':
ovalue = optarg;
break;
default:
abort ();
}
printf ("fvalue = %s, ovalue = %s\n",
fvalue, ovalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
你的optstring有误,一定是
while ((c = getopt (argc, argv, "f:o:")) != -1)
冒号表示该选项需要一个参数。
我想解析两个不同的命令行参数,这样:
file -f something -o something
我在互联网上找到了一些代码并对其进行了更改,但我无法解析它们。
int
main (int argc, char **argv)
{
char *fvalue = NULL;
char *ovalue = NULL;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "fo:")) != -1)
switch (c)
{
case 'f':
fvalue = optarg;
break;
case 'o':
ovalue = optarg;
break;
default:
abort ();
}
printf ("fvalue = %s, ovalue = %s\n",
fvalue, ovalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
你的optstring有误,一定是
while ((c = getopt (argc, argv, "f:o:")) != -1)
冒号表示该选项需要一个参数。