我很難讓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