|
楼主 |
发表于 2007-2-24 14:41:04
|
显示全部楼层
这里提供网络上一位兄弟的例子,这个例子可以对这个问题有一定的帮助
如果把"ab:c::"改成“”的话,就会发现optind一直等于1.- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- int main(int argc, char **argv)
- {
- int result;
- opterr = 0;
- while( (result = getopt(argc, argv, "ab:c::")) != -1 )
- {
- switch(result)
- {
-
- case 0:
- printf("case 0\n result=%c \n optind=%d ",result,optind);
- break;
- case 'a':
- printf("option=a, optopt=%c, optarg=%s\n", optopt, optarg);
- break;
- case 'b':
- printf("option=b, optopt=%c, optarg=%s\n", optopt, optarg);
- break;
- case 'c':
- printf("option=c, optopt=%c, optarg=%s\n", optopt, optarg);
- break;
- case '?':
- printf("result=?, optopt=%c, optarg=%s\n", optopt, optarg);
- break;
- default:
- printf("default, result=%c\n",result);
- break;
- }
- printf("argv[%d]=%s\n", optind, argv[optind]);
- }
- printf("result=-1, optind=%d argc=%d\n", optind,argc);
- for(result = optind; result < argc; result++)
- printf("-----argv[%d]=%s\n", result, argv[result]);
- for(result = 1; result < argc; result++)
- printf("\nat the end-----argv[%d]=%s\n", result, argv[result]);
-
- return 0;
- }
复制代码 |
|