当我运行我的程序./program a b c d
而不是
./program -i inFile -o outFile
它告诉我文件打开有问题(这是真的)但是
Expected: "Usage: program -i inputfile -o outputfile\n"
Got: "Error: Cannot open file /no/such/file\n"
你知道我该怎么处理吗?有什么线索吗?这也是我处理错误参数处理的代码的一部分:
if ((s= strrchr( argv[0], '\\')) /* get filename w/o .exe extension */
|| (s= strrchr( argv[0], '/')))
s++;
else
s= argv[0];
if(inFile == NULL || outFile == NULL) {
error_usage(s);
}
if ( argc !=5 )
{
error_usage(s);
return -1;
}
while ((c = getopt(argc, argv, "i:o:")) != -1) {
switch (c) {
case 'i':
inFile = strdup(optarg);
break;
case 'o':
outFile = strdup(optarg);
break;
default:
error_usage(s);
}
}
if (!(iFile = fopen(inFile, "r+"))) {
fprintf(stderr, "Error: Cannot open file %s\n", inFile);
exit(1);
}