• <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

            統計

            • 隨筆 - 85
            • 文章 - 0
            • 評論 - 17
            • 引用 - 0

            常用鏈接

            留言簿(6)

            隨筆分類

            隨筆檔案

            收藏夾

            全是知識啊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            C語言:getopt_long()函數的作用
                Linux系統下,需要大量的命令行選項,如果自己手動解析他們的話實在是有違軟件復用的思想,不過還好,GNU C library留給我們一個解析命令行的接口(X/Open規范),好好使用它可以使你的程序改觀不少。
                使用getopt_long()需要引入頭文件:#include<getopt.h>
                現在我們使用一個例子來說明它的使用。
                一個應用程序需要如下的短選項和長選項
                短選項       長選項                 作用
                -h           --help             輸出程序命令行參數說明然后退出
                -o filename  --output filename  給定輸出文件名
                -v           --version          顯示程序當前版本后退出
                為了使用getopt_long()函數,我們需要先確定兩個結構:
                1.一個字符串,包括所需要的短選項字符,如果選項后有參數,字符后加一個":"符號。本例中,這個字符串應該為"ho:v"。(因為-o后面有參數filename,所以字符后面需要加":")。
                2. 一個包含長選項字符串的結構體數組,每一個結構體包含4個域,第一個域為長選項字符串,第二個域是一個標識,只能為0或1,分別代表沒有選項或有選項。第三個域永遠為NULL。第四個選項域為對應的短選項字符串。結構體數組的最后一個元素全部位NULL和0,標識結束。在本例中,它應為以下的樣子:
                const struct option long_options[] = {
                    {"help", 0, NULL, 'h'},
                    {"output", 1, NULL, 'o'},
                    {"version", 0, NULL, 'v'},
                    {NULL, 0, NULL, 0}
                };
                 調用時需要把main的兩個參數argc和argv以及上述兩個數據結構傳給getopt_long()函數。
            #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 隨意門 閱讀(7918) 評論(0)  編輯 收藏 引用

            久久午夜无码鲁丝片| 久久狠狠色狠狠色综合| 亚洲人成网站999久久久综合| 久久精品国产精品亚洲| 久久久久国产视频电影| 亚洲美日韩Av中文字幕无码久久久妻妇| 亚洲精品tv久久久久久久久久| 国产精品亚洲综合久久| 色欲综合久久躁天天躁蜜桃| 久久精品国产一区| 一本色综合久久| 婷婷久久综合九色综合98| 亚洲美日韩Av中文字幕无码久久久妻妇| 亚洲AV日韩精品久久久久久久| 国产成人无码精品久久久免费| 久久久久亚洲AV成人网人人网站 | 久久精品人妻中文系列| 国产午夜福利精品久久2021 | 久久一区二区三区99| 久久精品中文无码资源站| 久久人妻少妇嫩草AV蜜桃| 97精品伊人久久大香线蕉app| 欧美一级久久久久久久大片| 久久久九九有精品国产| 久久久精品国产免大香伊 | 国产精品久久久久久久久软件| 91精品国产高清久久久久久国产嫩草| 久久精品无码免费不卡| 久久综合亚洲色一区二区三区| 国产视频久久| 久久这里只有精品首页| 国产午夜福利精品久久2021| 亚洲色大成网站www久久九| 伊人久久大香线蕉AV一区二区| 国产99久久久久久免费看| 99久久精品国产高清一区二区| 久久精品国产免费观看| 久久婷婷午色综合夜啪| 亚洲综合久久久| 无码日韩人妻精品久久蜜桃| 无码人妻精品一区二区三区久久 |