[c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释
getopt被用来解析命令行选项参数。
#include <unistd.h>
extern char *optarg; //选项的参数指针
extern int optind, //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
extern int opterr, //当opterr=0时,getopt不向stderr输出错误信息。
extern int optopt; //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt 中,getopt返回'?’
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> #ifdef RDP2
#define VNCOPT "V:Q"
#else
#define VNCOPT "V:"
#endif int main(int argc, char **argv)
{
int ch, i;
opterr = ; while ((ch = getopt(argc,argv, VNCOPT ":a:bcde::f"))!=-)
{
printf("optind:%d,opterr:%d\n", optind, opterr);
switch(ch)
{
case 'V':
printf("option V:'%s'\n",optarg);
break;
case 'a':
i = strtol(optarg, NULL, );
printf("i = %x\n", i);
printf("option a:'%s'\n",optarg);
break;
case 'b':
printf("option b :b\n");
break;
case 'e':
printf("option e:'%s'\n",optarg);
break;
default:
printf("other option :%c\n",ch);
}
}
for(int i = ; i < argc; i++)
printf("argv[%d] = [%s]\n", i, argv[i]);
printf("argc = [%d]\n", argc);
printf("optopt +%c\n",optopt); return ;
}
执行结果:
$ ./getopt -a -bcd -e
i =
option a:''
optind:,opterr:
option b :b
optind:,opterr:
other option :c
optind:,opterr:
other option :d
optind:,opterr:
option e:''
optind:,opterr:
argv[] = [./getopt]
argv[] = [-a]
argv[] = []
argv[] = [-bcd]
argv[] = [-e]
argv[] = []
argc = []
optopt +e
optind =
argv 数组存储命令行字符串(包含./getopt执行程序)
argc 命令行字符串个数(./getopt -a 123 -bcd -e 456) 6个
optind 下一次调用getopt的时,从optind存储的位置处重新开始检查选项。从0开始计算
(对应下面位置)
./getopt -a 123 -bcd -e 456
0 1 2 3 4 5 6
[c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释的更多相关文章
- main(int argc, char **argv)参数解读
main(int argc, char **argv)参数解读 编译生成了test.exe ,然后在控制台下相应的目录下输入:test 1 2 3 4 argc就是一个输入了多少个参数,包括te ...
- int main(int argc,char* argv[])参数详解
argc是命令行总的参数个数 argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数 命令行后面跟的用户输入的参数,比如: int main(int argc, char* argv[] ...
- int main(int argc,char *argv[])参数的应用
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/sta ...
- main()函数的输入参数 main(int argc, char** argv)
一般简单的C++程序,main函数的写法都是 int main() {... ; return 0;},但是,如果在运行程序时需要有参数输入,可以是使用将主函数写成int main(int argv, ...
- 解决 main(int argc, char** argv)这种情况下通过命令行传参,改为main函数里面给定参数。
本文是原创文章,未经允许,请勿转载. 原来程序中是通过运行exe,然后加上参数,然程序运行起来的.也就是main(int argc, char** argv)这里是通过argv参数是从命令行 传过来的 ...
- C++中 int main(int argc, char **argv) 命令行传递参数
C++中,比较常见的是不带参数的主函数int main(),如果使用命令行执行程序,主函数也可以接收预先输入的参数,形式如下. int main(int argc,char **argv) argc: ...
- [笔记] 命令行参数 int main(int argc,char *argv[])
int main(int argc,char *argv[]) // argument count 变量个数 argument values 变量值 C程序的main函数有两个形参* argc:整数, ...
- 关于int main( int argc, char* argv[] ) 中arg和argv参数的解析及调试
https://blog.csdn.net/LYJ_viviani/article/details/51873961 https://stackoverflow.com/questions/30241 ...
- int main(int argc,char* argv[]) 的含义和用法
1.基本概念 argc,argv 用命令行编译程序时有用. 主函数main中变量(int argc,char *argv[ ])的含义,有些编译器允许将main()的返回类型声明为void,这已不再是 ...
随机推荐
- HTML5简介、视频
HTML5 建立的一些规则: 新特性应该基于 HTML.CSS.DOM 以及 JavaScript. 减少对外部插件的需求(比如 Flash) 更优秀的错误处理 更多取代脚本的标记 HTML5 应该独 ...
- POJ2446 二分图最大匹配
问题:POJ2446 分析: 采用黑白相间的方法把棋盘分成两个点集,是否可以用1*2的卡片实现全覆盖等价于二分图是否有完全匹配. AC代码 //Memory: 172K Time: 32MS #inc ...
- Windows Server 2003 SP2企业版ISO下载, windows2003系统下载,2003系统下载,2003系统
Windows Server 2003 SP2 企业版ISO下载(真正企业免激活版) 此版本适合作为一个新系统来安装,也适合在虚拟机中安装 点评:Windows Server 2003 SP2 企业版 ...
- mvc4 基于Area实现插件模块化开发
对于一个较大规模的Web应用,可以从功能上通过Area将其划分为为较小的单元.每个Area相当于一个独立的子系统,具有一套包含Model.Views和Controller在内 的目录结构和配置文件.一 ...
- 利用Azure高级存储搭建高性能Linux服务器(2)
我们首先来测试随机写的IOPS,可以看到随机写的IOPS可以达到,顺序写的IOPS可以达到: $ sudo fio -filename=/data/testfile -direct=1 -iodept ...
- LeetCode_Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- 使用opencv传中文文件崩溃
这个问题经过我的调试发现: 程序是在 while (*at && !isdigit(*at)) at++; 这个语句时crash的,但是跟进去是isdigit的问题,因为变量a ...
- libeXosip2(1-2) -- How-To initiate, modify or terminate calls.
How-To initiate, modify or terminate calls. The eXtented eXosip stack eXosip2 offers a flexible API ...
- Gray Code 解答
Question The gray code is a binary numeral system where two successive values differ in only one bit ...
- 构建高性能WEB站点笔记三
构建高性能WEB站点笔记三 第10章 分布式缓存 10.1数据库的前端缓存区 文件系统内核缓冲区,位于物理内存的内核地址空间,除了使用O_DIRECT标记打开的文件以外,所有对磁盘文件的读写操作都要经 ...