2012. 11. 28. 05:25

[출처 :http://tran.kr/94 ] 

맨날 직접 짜다보면 빠트리는 구문이 생겨버리는 구절.. 아예 가지고 있어야 겠다.



  1: #include <stdio.h>
  2: #include <string.h>
  3: #include <stdlib.h>
  4: #include <unistd.h>
  5: 
  6: int main(int argc, char **argv)
  7: {
  8:    char  optstring[1024];
  9:    extern char *optarg;
 10:    int   optchar;
 11:    
 12:    
 13:    memset(optstring, 0x00, sizeof(optstring));
 14:    
 15:    sprintf(optstring, "%s", "d:h:");
 16:    
 17:    while((optchar = getopt(argc, argv, optstring)) != EOF)
 18:    {
 19:       
 20:       switch(optchar)
 21:       {
 22:          case 'd':
 23:             printf("option d = [%s] \n", optarg);
 24:             break;
 25:          
 26:          case 'h':
 27:             printf("option h = [%s] \n", optarg);
 28:             break;
 29:             
 30:          default :
 31:             break;
 32:       }
 33:    }
 34:    
 35:    printf("End\n");
 36:    
 37:    return 0;
 38: }

Posted by k1rha