| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5457 人关注过本帖
标题:一例说明 optind 到底是多少
只看楼主 加入收藏
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
结帖率:98.63%
收藏
 问题点数:0 回复次数:0 
一例说明 optind 到底是多少
程序代码:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
     
int main (int argc, char **argv) {
       int aflag = 0;
       int bflag = 0;
       char *cvalue = NULL;
       int index;
       int c;
     
       opterr = 0;
     
       while ((c = getopt (argc, argv, "abc:")) != -1)
              switch (c) {
                   case 'a':
                        aflag = 1;
                        break;
                   case 'b':
                        bflag = 1;
                        break;
                   case 'c':
                         cvalue = optarg;
                         break;
                   case '?':
                          if (optopt == 'c')
                               fprintf (stderr, "Option -%c requires an argument.\n", optopt);
                         else if (isprint (optopt))
                               fprintf (stderr, "Unknown option `-%c'.\n", optopt);
                         else
                               fprintf (stderr, "Unknown option character `\\x%x'.\n",  optopt);
                         return 1;
                   default:
                         abort ();
           }
     
       printf ("aflag = %d, bflag = %d, cvalue = %s\n", aflag, bflag, cvalue);
     
       for (index = optind; index < argc; index++) {
            printf("%d\t",index);
            printf ("Non-option argument %s\n", argv[index]);
        }
       return 0;
}


$ ./a.out  -a abc -c ddd eee

aflag = 1, bflag = 0, cvalue = (null)

2    Non-option argument abc

3    Non-option argument -c

4    Non-option argument ddd

5    Non-option argument eee

$ ./a.out  -c abc -a ddd eee

aflag = 1, bflag = 0, cvalue = abc

4    Non-option argument ddd

5    Non-option argument eee

前面的数字示明 optind 的变化


[ 本帖最后由 madfrogme 于 2012-8-25 09:10 编辑 ]
搜索更多相关主题的帖子: include abc 多少 
2012-08-25 08:06
快速回复:一例说明 optind 到底是多少
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017888 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved