getopt()を使用してコマンドライン引数を解析しようとしています。以下は私のコードです。 getopt()は、プログラムの実行時に渡す引数に関係なく常に-1を返します。例えばgetoptは常に-1を返しますgetoptは何もしません
:
$ gcc -o test test.c
$ ./test f
誰も私が間違っているのかを見ることができますか?ありがとうございました。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
void usage (char * progname)
{
fprintf(stderr, "Usage Instructions Here ...\n");
exit(-1);
}
int main (int argc, char *argv[])
{
int opt;
while((opt = getopt(argc, argv, "?hf:")) != -1) {
switch(opt) {
case '?':
case 'h':
usage(argv[0]);
break;
case 'f':
{
FILE *fp;
char *filename = strdup(optarg);
if((fp = fopen(filename, "r")) == NULL) {
usage(argv[0]);
}
}
break;
default:
fprintf(stderr, "Error - No such opt, '%c'\n", opt);
usage(argv[0]);
}
}
return(0);
}
だから、どのように実行しているのですか? –
オプションは '-'または' --'で始まります。 –