我很难让getopt操作可选。这是我的代码的一部分。它需要一个参数文件,如果它在那里,它会计算字符的数量。如果不是,则计数stdinput字符。可选的getopt函数
我的问题是什么optarg一旦它是未申报设置?我怎么去做我的选项-c可选,并使其工作。
目前它总是从标准输入读取。
while((option = getopt(argc, argv, "c::")) != -1) {
switch(option) {
case 'c':
if (optarg == NULL) {
file = stdin;
}
else {
file = fopen(optarg, "r");
}
while((ch = fgetc(file)) != EOF) {
count++;
}
printf("%d %s\n", count, optarg);
fclose(file);
break;
“什么'optarg'设置为一旦它是未申报”???你不可能比这更不清楚。 – 2014-11-05 08:05:28
为什么'::',而不是':'? – alk 2014-11-05 08:17:55
“两个冒号意味着一个选项需要一个可选的参数” - linuxmanual – Sarc 2014-11-05 08:21:55