ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 프로그램 옵션 처리 예제
    System Programming 2007. 11. 1. 16:10
    728x90
    #include <unistd.h>
    #include <stdio.h>
    #define VERSION "0.0.1"
    void print_help();

    void main(int argc, char *argv[])
    {
     int index,c;

     opterr=0;
     if (argc == 1)
         print_help();
     while((c=getopt(argc, argv,"hvf:"))!=-1)
         switch(c) {
            case 'h':
                print_help();
                exit(0);
            case 'v':
                printf("testopt %s\n",VERSION);
                exit(0);
            case 'f':
                printf("File: %s\n",optarg);
                break;
            case '?':
                fprintf(stderr, "Unknown option '-%c'.\n",optopt);
                print_help();
                exit(1);
            }
        for (index = optind; index < argc; index++)
            printf("Non-option argument %s\n", argv[index]);
    }

    void print_help()
    {
        printf("testopt (ver.%s) is a getopt example.\n", VERSION);
        printf("testopt [-h] [-v] [-f FILE].\n");
        printf("  -h        print this help and exit\n");
        printf("  -v        print virsion and and exit\n");
        printf("  -f FILE    set file\n");
    }

    'System Programming' 카테고리의 다른 글

    qsort  (0) 2007.11.01
    lsearch / lfind  (0) 2007.11.01
    리눅스 로그인 사용자 명령어  (1) 2007.09.29
    select shell script  (0) 2007.09.20
    실습 보고서 작성  (0) 2007.09.06

    댓글

Designed by black7375.