首页 > 系统服务 > 详细

linux getopt使用之解析输入字符串

时间:2020-09-10 16:32:46      阅读:60      评论:0      收藏:0      [点我收藏+]


处理:
第二个参数有些东西: :后面须接参数 -bxx和:: 后面可不接参数,后面多个选项,-mnzxc
argv[optind]:可以得到后面 -axx -bxx 后面这个-bxx的值,不过是string类型的.
optarg:得到是-a后面的值.


#include <stdio.h> #include <unistd.h> #include "task.h" int main(int argc,char**argv) { int ch = -1; char *string = "v::a::b::cdezgnmcv"; int count = 0; while((ch=getopt(argc,argv,string))!=-1) { printf(" is %c\n",ch); printf("gain is %s\n",optarg); printf("optind is %d\n",optind); printf("string optind is %s\n",argv[optind]); printf("argc is %d\n",argc); printf("argv is %s\n",argv[1]); switch(ch) { case ‘a‘: printf("get opt ch is a\n"); break; case ‘b‘: printf("get opt ch is b\n"); break; case ‘c‘: printf("get opt ch is c\n"); break; case ‘d‘: printf("get opt ch is d\n"); break; case ‘n‘: printf("get opt is n\n"); break; case ‘v‘: printf(" string string gain is %s\n",optarg); break; default: printf("unknow\n"); break; } count++; printf("count is %d\n",count); } count = 0; //printf("argc backward is %d\n",argc); //task_create(); //printf("argc forward is %d\n",argc); while(1) { if( getchar() == ‘x‘) { printf("main phread\n "); break; } } return 0; }

 ./sunshine  -v500  -b11 

is v
gain is 500
optind is 2
string optind is -b11
argc is 3
argv is -v500
string string gain is 500
count is 1
is b
gain is 11
optind is 3
string optind is (null)
argc is 3
argv is -v500
get opt ch is b
count is 2

linux getopt使用之解析输入字符串

原文:https://www.cnblogs.com/nowroot/p/13645760.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!