码迷,mamicode.com
首页 > 其他好文 > 详细

6.可变参数问题-getopt函数

时间:2017-03-19 13:16:15      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:top   style   switch   while   记录   tar   检索   输出   信息   

 

 

 1 #include <unistd.h>
 2 #include <stdio.h>
 3 int main(int argc, char * argv[])
 4 {
 5     
 6     int ch;
 7     printf("\n\n");
 8     printf("the initial value of optind:%d, and opterr: %d\n",optind,opterr);   //2.用来记录下一个检索位置,3.是否将错误信息输出到stderr
 9     printf("--------------------------\n");
10     
11     while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
12     {
13            printf("optind: %d\n", optind);
14            switch (ch) 
15            {
16                case a:
17                        printf("HAVE option: -a\n\n");   
18                        break;
19                case b:
20                        printf("HAVE option: -b\n"); 
21                        printf("The argument of -b is %s\n\n", optarg);          //1.用来保存选项的参数
22                        break;
23                case c:
24                        printf("HAVE option: -c\n");
25                        printf("The argument of -c is %s\n\n", optarg);
26                        break;
27                case d:
28                    printf("HAVE option: -d\n");
29                      break;
30               case e:
31                     printf("HAVE option: -e\n");
32                     printf("The argument of -e is %s\n\n", optarg);
33                   break;
34               case ?:
35                        printf("Unknown option: %c\n",(char)optopt);             //4.不在字符串optstring中的选项
36                        break;
37            }
38     }
39 
40 
41 }

 

6.可变参数问题-getopt函数

标签:top   style   switch   while   记录   tar   检索   输出   信息   

原文地址:http://www.cnblogs.com/girlblooding/p/6579629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!