2017-09-13 1 views
0

잘못된 옵션/명령을 입력해도 while 루프를 입력하지 않으면 프로그램이 기본 대소 문자를 사용합니다. 나는 내가 뭘 잘못하고 있는지 궁금 하네. 제대로 작동하기 위해서는 내가 바꿀 필요가있는 것이있다. 올바른 경우에만 사용된다. :) 의견에서C - getopt 스위치 while 루프와 잘못된 옵션의 기본 경우

#include <stdio.h> 
#include <stdlib.h> 
#include <getopt.h> 
#include <string.h> 
int main (int argc, char *argv[]){ 

const char *optstring = "rs:pih"; 
char option; 

while ((option = getopt(argc, argv, optstring)) != EOF) { 

printf("I'm in the while loop!"); 

    switch (option) { 
     case 'r': 
      break; 
     case 's': 
      printf("Second Argument: %s", optarg); 
      break; 
     case 'p': 
      printf("p"); 
      break; 
     case 'i': 
      printf("i"); 
      break; 
     case 'h': 
      printf("h"); 
      break; 
     default: 
      printf("nothing"); 

    } 

} 

return 0; 
} 
+0

을 // www가 .gnu.org/software/libc/manual/html_node/Example-of-Getopt.html # Getopt 예제 –

+0

나를 위해 완벽하게 잘 작동하는 것 같습니다. 테스트 케이스를 게시하십시오. – Dolda2000

+0

나의 테스트 케이스는 ./program_name d – John

답변

0

는 :

./program_name d

은 깃발이 없습니다. 시도 : ./program_name -r -s foo는 -p

당신은 또한을 통해 구문 분석되지 않은 나머지 옵션 인쇄 할 수 있습니다 :은 https : 당신은이에 alook 걸릴 수 있습니다

for (int i = optind; i < argc; i++) { 
    printf("remaining argument[%d]: %s\n", i, argv[i]); 
}