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);
}
그래서 .. 어떻게 실행하는지 몇 가지 예가 무엇입니까? –
옵션은'-' 또는'--'로 시작해야합니다. –