使用的getopt我希望把一個子選項中的字符串,這樣我可以把它當作一個文件名讀取文件:在命令行,如果我輸入的選擇在C
char *nvalue = NULL;
char *dvalue = NULL;
char *input = NULL;
char inputfilename[] = "\"";
int ar;
int main(int argc, char *argv[])
{
while ((ar = getopt(argc, argv, "hn:d:i:")) != -1)
switch (ar)
{
case 'h':
printf("something");
break; /* added */
case 'n':
nvalue = optarg;
if (isdigit(nvalue))
stop = atoi(nvalue);
else
printf("something\n");
break; /* added */
case 'd':
dvalue = optarg;
if (!strcmp(dvalue, "FCFS") || !strcmp(dvalue, "SSTF") ||
!strcmp(dvalue, "C-SCAN") || !strcmp(dvalue, "LOOK"))
;
else
printf("Invalid type of disk scheduling policy entered.\n");
break; /* added */
case 'i':
input = optarg;
strcpy(inputfilename, optarg);
printf("Filename :%s\n", inputfilename);
break;
}
/* ... */
}
所以:
./foobar -i hello
那麼我應該能夠讀取該文件:
FILE *file = fopen(inputfilename, "r");
有什麼建議?答案? 謝謝!
這段代碼有什麼問題?我添加了中斷來切換選項,聲明停止變量幷包含適當的文件,並按照您的要求工作。所以我不知道我該怎麼幫你。 – Patryk