• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            興海北路

            ---男兒仗劍自橫行
            <2010年6月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            統(tǒng)計(jì)

            • 隨筆 - 85
            • 文章 - 0
            • 評(píng)論 - 17
            • 引用 - 0

            常用鏈接

            留言簿(6)

            隨筆分類

            隨筆檔案

            收藏夾

            全是知識(shí)啊

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            C語言:getopt_long()函數(shù)的作用
                Linux系統(tǒng)下,需要大量的命令行選項(xiàng),如果自己手動(dòng)解析他們的話實(shí)在是有違軟件復(fù)用的思想,不過還好,GNU C library留給我們一個(gè)解析命令行的接口(X/Open規(guī)范),好好使用它可以使你的程序改觀不少。
                使用getopt_long()需要引入頭文件:#include<getopt.h>
                現(xiàn)在我們使用一個(gè)例子來說明它的使用。
                一個(gè)應(yīng)用程序需要如下的短選項(xiàng)和長(zhǎng)選項(xiàng)
                短選項(xiàng)       長(zhǎng)選項(xiàng)                 作用
                -h           --help             輸出程序命令行參數(shù)說明然后退出
                -o filename  --output filename  給定輸出文件名
                -v           --version          顯示程序當(dāng)前版本后退出
                為了使用getopt_long()函數(shù),我們需要先確定兩個(gè)結(jié)構(gòu):
                1.一個(gè)字符串,包括所需要的短選項(xiàng)字符,如果選項(xiàng)后有參數(shù),字符后加一個(gè)":"符號(hào)。本例中,這個(gè)字符串應(yīng)該為"ho:v"。(因?yàn)?o后面有參數(shù)filename,所以字符后面需要加":")。
                2. 一個(gè)包含長(zhǎng)選項(xiàng)字符串的結(jié)構(gòu)體數(shù)組,每一個(gè)結(jié)構(gòu)體包含4個(gè)域,第一個(gè)域?yàn)殚L(zhǎng)選項(xiàng)字符串,第二個(gè)域是一個(gè)標(biāo)識(shí),只能為0或1,分別代表沒有選項(xiàng)或有選項(xiàng)。第三個(gè)域永遠(yuǎn)為NULL。第四個(gè)選項(xiàng)域?yàn)閷?duì)應(yīng)的短選項(xiàng)字符串。結(jié)構(gòu)體數(shù)組的最后一個(gè)元素全部位NULL和0,標(biāo)識(shí)結(jié)束。在本例中,它應(yīng)為以下的樣子:
                const struct option long_options[] = {
                    {"help", 0, NULL, 'h'},
                    {"output", 1, NULL, 'o'},
                    {"version", 0, NULL, 'v'},
                    {NULL, 0, NULL, 0}
                };
                 調(diào)用時(shí)需要把main的兩個(gè)參數(shù)argc和argv以及上述兩個(gè)數(shù)據(jù)結(jié)構(gòu)傳給getopt_long()函數(shù)。
            #include <getopt.h>
            #include 
            <stdio.h>
            #include 
            <stdlib.h>

            /* The name of this program. */
            const char* program_name;

            /* Prints usage information for this program to STREAM (typically
               stdout or stderr), and exit the program with EXIT_CODE. Does not
               return. 
            */

            void print_usage(FILE* stream, int exit_code)
            {
                fprintf(stream, 
            "Usage: %s options [ inputfile ]\n"
                    program_name);
                fprintf(stream, 
                    
            "  -h  --hlep          Display this usage information.\n"
                    
            "  -o  --output filename Write output to file.\n"
                    
            "  -v  --verbose         Print verbose message.\n");
                exit (exit_code);
            }


            /* Main program entry point. ARGC contains number of argument list
               elements; ARGV is an array of pointers to them. 
            */


            int main(int argc, char *argv[])
            {
                
            int next_option;

                
            /* A string listing valid short options letters. */
                
            const char* const short_options = "ho:v";
                
                
            /* An array describing valid long options. */
                
            const struct option long_options[] = {
                    
            {"help"0, NULL, 'h'},
                
            {"output"1, NULL, 'o'},
                
            {"verbose"0, NULL, 'v'},
                
            {NULL, 0, NULL, 0}    /* Required at end of array. */
                }
            ;

                
            /* The name of the file to receive program output, or NULL for
                 * standard output. 
                 
            */

                
            const char* output_filename = NULL;

                
            /* Whether to display verbose messages. */
                
            int verbose = 0;

                
            /* Remember the name of the program, to incorporate in messages.
                 * The name is stored in argv[0].
                 
            */

                program_name 
            = argv[0];

                
            do {
                    next_option 
            = getopt_long (argc, argv, short_options,
                    long_options, NULL);
                
            switch (next_option)
                
            {
                    
            case 'h':    /* -h or --help */
                    
            /* User has requested usage information. Print it to
                     * standard output, and exit with exit code zero
                     * (normal termination).
                     
            */

                    print_usage(stdout, 
            0);

                    
            case 'o':    /* -o or --output */
                    
            /* This option takes an argument, the name of the
                     * output file.
                     
            */

                    output_filename 
            = optarg;
                    
            break;

                    
            case 'v':    /* -v or --verbose */
                    verbose 
            = 1;
                    
            break;

                    
            case '?':    /* The user specified an invalid option. */
                    
            /* Print usage information to standard error, and exit
                     * with exit code one (indicating abnormal
                     * termination).
                     
            */

                    print_usage(stderr, 
            1);

                    
            case -1:    /* Done with options. */
                    
            break;

                    
            default:    /* Something else: unexpected. */
                    abort();
                }

                }
             
                
            while (next_option != -1);

                
            /* Done with options. OPTINO points to first nonoption argument.
                 * FOr demonstration purposes, print them if the verbose option
                 * was specified.
                 
            */

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


                
            /* The main program goes here. */
                
            return 0;
            }

               

            posted on 2010-06-13 15:32 隨意門 閱讀(7934) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久久精品视频免费观看| 亚洲七七久久精品中文国产| 精品久久久久久中文字幕| 久久久久久九九99精品| 久久成人永久免费播放| 国产精品免费看久久久| 国产激情久久久久影院小草 | 日韩人妻无码一区二区三区久久 | 99久久99久久精品免费看蜜桃| 99久久国产热无码精品免费久久久久| 四虎国产精品免费久久| 九九久久99综合一区二区| 久久久久久曰本AV免费免费| 四虎国产精品免费久久5151 | 日韩亚洲欧美久久久www综合网| 久久精品国产男包| 99久久精品无码一区二区毛片 | 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 精品熟女少妇a∨免费久久| 人妻无码精品久久亚瑟影视| 嫩草影院久久国产精品| 嫩草伊人久久精品少妇AV| 偷窥少妇久久久久久久久| 久久精品中文字幕有码| 久久国产精品无码一区二区三区 | 9久久9久久精品| 午夜精品久久久久久久久| 国产精品久久久香蕉| 亚洲性久久久影院| 亚洲国产日韩欧美久久| 热久久国产欧美一区二区精品| 国产精品亚洲美女久久久| 久久最近最新中文字幕大全| 狠狠久久亚洲欧美专区| 精品久久久久久中文字幕| 99久久国产综合精品麻豆| 国产麻豆精品久久一二三| 久久国产精品成人免费| 久久亚洲精品视频| 国产午夜电影久久| 午夜福利91久久福利|