0
啓動時失敗這是一個使用getopt_long(),所以請原諒我,如果這個問題是微不足道的我的第一個項目。getopt_long()與非選項字符
我有一個問題,當傳遞給我的程序的第一個參數是無效的
這裏是我的主
int main(int argc, char * argv[])
{
printf("----------------------------------------------\n\n");
int fd[128];
int fdCount=0;
int c;
int digit_optind =0;
int verboseFlag=0;
//to exit with this specific value collected from all functions
int overallExitStatus=0;
while(1)
{
int this_option_optind = optind ? optind : 1;
int option_index =0;
static struct option long_options[] = {
{"rdonly", optional_argument, 0, 'a'},
{"wronly", optional_argument, 0, 'b'},
{"command", optional_argument, 0, 'c'},
{"verbose", no_argument, 0, 'd'},
{0,0,0,0}
};
c=getopt_long(argc,argv, "+", long_options, &option_index);
if(c == -1)
break;
switch(c) {
case 'a':
rdonly(fd,&fdCount,verboseFlag,&optind,argc,argv);
break;
case 'b':
wronly(fd,&fdCount,verboseFlag,&optind,argc,argv);
break;
case 'c':
overallExitStatus +=command(fd,&optind,optarg,argc,argv,verboseFlag);
break;
case 'd':
verboseFlag=1;
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}
printf("Program is finished exiting with status %d\n",overallExitStatus);
printf("----------------------------------------------\n\n");
return overallExitStatus;
}
基本代碼,如果我以下列方式
開始我的計劃./myProgram一個--rdonly FILE1.TXT
程序不解析低谷任何參數,將跳過開關,徑直返回。
的情況下當第一個參數開始與 - 或 - (即使它是一個錯誤的說法)程序正確的行爲。
我怎樣才能解決這個問題?
謝謝。