getopt和getopt_long
来源:互联网 发布:wepy 知乎 编辑:程序博客网 时间:2024/06/05 18:20
getopt和getopt_long函数
1.介绍getopt
int getopt(int argc, char * const argv[], const char *optstring);
getopt函数的前两个参数,就是main函数的argc和argv,这两者直接传入即可,要考虑的就只剩下第三个参数。
optstring的格式举例说明比较方便,例如:
char *optstring = “abcd:”;
上面这个optstring在传入之后,getopt函数将依次检查命令行是否指定了 -a, -b, -c及 -d(这需要多次调用getopt函数,直到其返回-1),c才能检查完所有你输入的参数。因为每次检查一个参数,当检查到上面某一个参数被指定时,函数会返回被指定的参数名称(即该字母)
最后一个参数d后面带有冒号,代表可以加指定的参数,如 -d 10086
optind表示的是下一个将被处理到的参数在argv中的下标值。
如果指定opterr = 0的话,在getopt、getopt_long遇到错误将不会输出错误信息到标准输出流。
代码和运行结果:
2介绍getopt_long函数
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
2.1
在这里第一个多余的参数:longopts指向的是一个由option结构体组成的数组,这是已经定义好的,你动态调用就行。
结构如下:
struct option { const char *name; //是参数的名称 int has_arg; //无参0有参1都行2 int *flag; //flag = 0函数返回val int val; //指定函数找到选项时候的番薯返回值或者flag指向的值 };
2.2
如果longindex不为,0,,即是找到参数在longopts中的下标值。
代码:
结果:
阅读全文
0 0
- getopt_long和 getopt
- getopt、getopt_long和getopt_long_only
- getopt和getopt_long
- getopt和getopt_long函数
- getopt()和getopt_long()用法
- getopt、getopt_long和getopt_long_only
- getopt、getopt_long和getopt_long_only
- getopt()和getopt_long()用法
- getopt()和getopt_long()用法
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- vue阻止事件冒泡
- JAVA中的Pattern
- Python 深入浅出
- VS2017常见问题解决
- 如何做出漂亮的序列比对图——ENDscript/ESPript
- getopt和getopt_long
- gwo灰狼算法matlab源码
- 计算机语言之战
- vuejs进阶之路一基础知识 2017-12-16
- git Authentication failed解决办法
- Python Json树更新
- mysql服务突然丢失解决方案(转载) 已测试解决问题
- HDU 3790 最短路问题(最短路)
- http协议常见状态码解读