免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1215 | 回复: 2
打印 上一主题 下一主题

关于getopt()。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-18 19:38 |只看该作者 |倒序浏览
我在一本书上看到:如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符。
但是程序显示好像不是这样:
  1. #include<stdio.h>;
  2. #include<unistd.h>;

  3. extern char *optarg;
  4. extern int optind, optopt,opterr;

  5. int main(int argc,char **argv)
  6. {
  7.         int ch;

  8.         while((ch = getopt(argc,argv,"a:bcd")) != -1)
  9.         switch(ch)
  10.         {
  11.         case 'a':
  12.                 printf("option a:'%s'\n",optarg);
  13.                 break;
  14.         case 'b':
  15.                 printf("option b :b\n");
  16.                 break;
  17.         case '?':

  18.                 printf("other option :%c\n",ch);
  19.         }

  20.         printf("optopt +%c\n",optopt);
  21. }
复制代码

./getopt_test -w
./getopt_test: invalid option -- w
other option
optopt +w
看来好像是:如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为不符的那个字符才对。
是不是这样啊?

论坛徽章:
0
2 [报告]
发表于 2005-04-20 12:56 |只看该作者

关于getopt()。

你理解有点偏差可能。使用“?”的场合是指你在一个opt后面本该跟参数,但你没有跟,此时则会进入“?”分支。如果你getopt中根本没有制定参数(如“w”)则与此无关。

论坛徽章:
0
3 [报告]
发表于 2005-04-20 13:18 |只看该作者

关于getopt()。

getopt是做什么用的?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP