webbench学习之getopt_long,atio
来源:互联网 发布:网络维护培训学校 编辑:程序博客网 时间:2024/05/22 06:57
webben在main函数中使用了getopt_long函数获得main函数的输入参数,可以看到文档中的参数定义
int getopt(int argc, char * const argv[], const char *optstring);
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
getopt 和 getopt_long的区别在于getopt_long比getopt函数多两个参数,首先介绍getopt函数的三个参数定义:
argc 和argv就main函数的输入,optstring 是用户自定义的字符串,下面举例说明
int main(int argc, char *argv[]) { int opt; while((opt=getopt(argc, argv, "c:d:e:"))!=EOF) { printf("opt is %c",opt); printf("\n optarg %s",optarg); printf("\n optind %d", optind); printf("\n optargv[] %s", argv[optind-1]); } }
"c:d:e:"时,getopt函数依次检查输入中是否有-c -d -e,:的意思是在这些参数后面有其他的输入,如-c 100 -d 200 -e 300
函数输出
[gaowei@iZ28n8zvqheZ ctest]$ ./a.out -c 100 -d 200 -e 300opt is c optarg 100 optind 3 optargv[] 100opt is d optarg 200 optind 5 optargv[] 200opt is e optarg 300 optind 7 optargv[] 300
getopt_long函数的longopts的参数是一个 struct option 的结构体,结构体定义
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
name 是参数的名称
has_arg 指明是否带参数值,其数值可选:
no_argument (即 0) 表明这个长参数不带参数(即不带数值,如:--name)
required_argument (即 1) 表明这个长参数必须带参数(即必须带数值,如:--name Bob)
optional_argument(即2)表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
flag 当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0
val 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。
webbench程序中的定义为
static const struct option long_options[]={ {"force",no_argument,&force,1}, {"reload",no_argument,&force_reload,1}, {"time",required_argument,NULL,'t'}, {"help",no_argument,NULL,'?'}, {"http09",no_argument,NULL,'9'}, {"http10",no_argument,NULL,'1'}, {"http11",no_argument,NULL,'2'}, {"get",no_argument,&method,METHOD_GET}, {"head",no_argument,&method,METHOD_HEAD}, {"options",no_argument,&method,METHOD_OPTIONS}, {"trace",no_argument,&method,METHOD_TRACE}, {"version",no_argument,NULL,'V'}, {"proxy",required_argument,NULL,'p'}, {"clients",required_argument,NULL,'c'}, {NULL,0,NULL,0}};
另一个参数longindex,如果longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值。
综上所述,webbench程序在进入main函数后就判断了main函数的参数
while((opt=getopt_long(argc,argv,"912Vfrt:p:c:?h",long_options,&options_index))!=EOF ) { switch(opt) { case 0 : break; case 'f': force=1;break; case 'r': force_reload=1;break; case '9': http10=0;break; case '1': http10=1;break; case '2': http10=2;break; case 'V': printf(PROGRAM_VERSION"\n");exit(0); case 't': benchtime=atoi(optarg);break; case 'p': /* proxy server parsing server:port */ tmp=strrchr(optarg,':'); proxyhost=optarg; if(tmp==NULL) { break; } if(tmp==optarg) { fprintf(stderr,"Error in option --proxy %s: Missing hostname.\n",optarg); return 2; } if(tmp==optarg+strlen(optarg)-1) { fprintf(stderr,"Error in option --proxy %s Port number is missing.\n",optarg); return 2; } *tmp='\0'; proxyport=atoi(tmp+1);break; case ':': case 'h': case '?': usage();return 2;break; case 'c': clients=atoi(optarg);break; } }
如果读入的opt为c 或者 t时,使用atoi函数,将读入的字符串转换成整数赋值给变量。
函数功能就是将数字表示的字符串转换成整数,识别的数字是从字符串起始位置到第一位非数字字符。
int main(){ char *p = "123.456"; int x = atoi(p); printf("%d",x);}
输出为:
123
- webbench学习之getopt_long,atio
- Webbench源码分析之参数输入getopt_long(一)
- Webbech学习之getopt_long函数
- Linux C之atio()函数
- webbench学习
- 程序参数解析函数使用getopt_long()函数,改编自webbench
- getopt_long函数学习笔记
- linux之getopt_long函数
- linux 之 getopt_long()
- linux之getopt_long函数
- STL之atio()数据类型转换 n-1位数
- webbench源码学习
- WebBench学习使用
- getopt_long
- getopt_long
- getopt_long
- getopt_long()
- getopt_long
- 黑马程序员——IO流(一)
- 史上最全设计模式导学目录(完整版)
- 基本的排序算法:冒泡排序、插入排序、希尔排序、选择排序、归并排序、快速排序、堆排序
- 常用ARM汇编指令
- eclipse 下github 安装
- webbench学习之getopt_long,atio
- 傅里叶变换、拉普拉斯变换、Z变换
- Jersey开发环境搭建(maven tomcat eclipse集成艰苦之旅)
- SDIO的SPI模式驱动分析
- 写博客了
- ios 开发中获取字符串中重复的字符的rang
- svn常见问题-----转自MyJavaBlog的专栏
- Android 换行
- android SrollView的内容发生变化时自动滚动的处理