ImageVerifierCode 换一换
你正在下载:

getopt.docx

[预览]
格式:DOCX , 页数:18 ,大小:20KB ,
资源ID:10640933      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10640933.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(getopt.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

getopt.docx

1、getopt函数名: getopt表头文件:#include 函数原型: int getopt(int argc, char * const argv,const char *optstring);所涉及的全局变量: extern char *optarg; extern int optind, opterr, optopt; 所涉及的静态的全局变量: static struct _getopt_data getopt_data函数描述: 1,getopt被用来解析命令行选项参数(短选项)。 2,argc,argv是在main函数调用的时候传递进来的命令行参数的个数和数组。 3,argv在传递

2、参数的时候每个选项字符以“-”(短选项)开始(比如是“* a 1 -b 2”),选项分为两种,一种是标志位,只需要选项本身,另一种是选项之后还要接受附加信息。 4,涉及到的全局变量: 4.1:extern char *optarg: 选项的参数指针,在检索到合法的选项字符之后,指向该字符后面的参数 4.2:extern int optind: 下一次调用getopt的时,从optind存储的位置处重新开始检查选项。每次成功的检索之后都从新赋值。一旦getopt找到了所有的选项且在参数中存在非选项字符的参数,则optind表示剩下的非选项参数的起点位置,如果在参数中没有非选项字符,则optind

3、指向所有命令行参数的末尾,默认值为1。 4.3:extern int opterr:当命令行选项字符不包括在optstring中或者选项缺少必要的参数时 会向stderr 打印错误信息( 比如编译之后的可执行文件为a.out 无效的选项是-u 则在终端执行的时候会显示./a.out: invalid option - u) 当opterr=0时,getopt不向stderr输出错误信息。默认值为1。 4.4:extern int optopt; 当命令行选项字符不包括在optstring中或者命令行选项字符包括在optstring中但是缺少必要的参数时,该选项存储在optopt中,getopt

4、返回?。如果命令行参数包括在opstring中,但是缺少必要的参数,而且optstring的第一个字符是 :,则此时返回的就是 :(默认值为?) ,我们可以输出此值作为诊断信息。 5,静态的全局变量(getopt.c文件中) static struct _getopt_data getopt_data;struct _getopt_data 和涉及到的全局变量有相同的名字和意义,在刚开始执行函数时候,全局变量会将数值付给静态的全局变量getopt_data中相应的成员变量,在函数执行结束之前该静态的全局变量的成员变量会将数据再付给相应的全局变量。 int optind; int opterr;

5、 int optopt; char *optarg;判断成员变量是否初始化,如果进行了初始化,该数字为TRUE,否则就是false int _initialized; 用来记录最后一次函数找到的选项字符,如果该变量为0或者指向一个空字符串,意味着通过移动到下一个命令行中的元素,重新开始扫描 char *_nextchar;描述在遇到非选项字符的时候怎么样去处理REQUIRE_ORDER 如果定义了环境变量POSIXLY_CORRECT,或者optstring的第一个字符为“+”,则就为该选项。如果设置为该值,则在遇到非字符选项的时候,函数会停止扫描。PREMUTE 默认的选项,如果遇到非选项字

6、符,会改变argv的顺序,以至于在最后,非选项字符都会在命令行参数的最后。RETURN_IN_ORDER: 如果optstring的第一个字符为“-”,则设置为该值,在函数的处理过程中会将每一个非选项字符都处理为ASCII码为1的字符的参数。 特殊参数 - 强制选项扫描结束,并且忽略_ordering的数值设置。在RETURN_IN_ORDER的情况下,只有 - 可以导致函数getopt在OPTIND!= ARGC的情况下返回-1,也就是强制的结束扫描。 enum REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER _ordering;判断环境变量POSIXLY_C

7、ORRECT是否进行了设置 int _posixly_correct;用来处理排列的参数,描述在argv中已经被跳过的非选项字符,_first_nonopt是他们其中的第一个,_last_nonopt是最后一个非选项字符后面的那个字符。 int _first_nonopt; int _last_nonopt;#if defined _LIBC & defined USE_NONOPTION_FLAGS int _nonoption_flags_max_len; int _nonoption_flags_len;# endif; 6,optstring为合法的选项字符(比如“ab”)以及+,-,

8、:,等特殊字符组成的字符串,参数的设置顺序和输入的顺序可以不一致。 如果字符后面跟着一个冒号,比如“a:” 这样意味着你再输入-a 的选项字符之后,还需要输入一个空格,然后添加一个参数,比如“* -a 100”,同时getopt会返回检索到的选项字符a,两个冒号意味着这是一个选项参数,在输入参数的时候不能在选项字符后面添加空格,而是直接添加参数,比如“* -a100”。 optstring也通常用来设置一些处理选项,比如,如果optstring的第一个参数为+或者设置了环境变量POSIXLY_CORRECT设置了,则在检索参数的时候,如果遇到没有在optstring中的选项字符,则函数会终止检

9、索。如果opstring的第一个参数为-则在遇到没有识别的选项字符时候,会将他们处理为ASCII码为1的字符的参数,特殊的参数”-”会强制结束扫描,无论是什么模式。 7,函数的返回值: 如果一个选项成功的找到,则函数返回的是该选项字符,如果所有的命令行参数都解析过了,则函数返回-1,如果遇到了没有在opstring中的选项或者找到了该选项字符,但是没有在该选项之后没有参数,返回“?”。如果在检索到合法的选项字符之后,但是后面没有参数的时候并且optstring的第一个字符为“:”,则此时函数返回“:”,如果opstring的第一个参数为-则在遇到没有识别的选项字符时候,会将他们处理为ASCII

10、码为1的字符的参数,也就是函数要返回1 。 如果在检索的时候,遇到没有在opestring中的选项字符的时候,函数会默认的将该字符的参数交换到命令行参数的最后,但是该字符的位置不变,仅仅是他的参数后换了。但是如果opstring的第一个参数为“+”或者POSIXLY_CORRECT设置了,这样在遇到不在optstring中的字符的时候 会停止扫描,也就是说他不会将选项缺少的参数移动到argv的末尾。如果此时你想利用这个空档耍滑的时候就太天真了,比如opstring为“a:b:c:”你输入的命令行参数为 “a 100 e 300 c” 这个时候你会说,不是在遇到-e的时候300会后换吗,那我不用

11、给-c赋值,这样-c的时候我读到的数值就是300了,但是你错了,你忽略的那个静态的全局变量get_data中的成员变量_first_nonopt的作用,这个时候“-c”的参数你依然是什么都读不到。实例程序:1-1 一个比较常规的用法:#include #include #include int main(int argc, char *argv) int result; opterr = 0; printf(0*n); while( (result = getopt(argc, argv, c:a:b:) != -1 ) switch(result) case a: printf(option

12、=a, optopt=%c %d, optarg=%sn, optopt,optopt, optarg); break; case b: printf(option=b, optopt=%c %d, optarg=%sn, optopt,optopt, optarg); break; case c: printf(option=c, optopt=%c %d, optarg=%sn, optopt,optopt, optarg); break; case ?: printf(option=?, optopt=%c %d, optarg=%sn, optopt,optopt, optarg);

13、break; case 1 : printf(the character is not defined!n); break; default: printf(default, result=%cn,result); break; printf(result is %d argv%d=%s optopt=%d-%cn,result, optind, argvoptind,optopt,optopt); printf(1*n); printf(result = %d optind = %dn,result,optind); printf(2*n); printf(the parameter aft

14、er optind:n); for(result = optind; result argc; result+) printf(-argv%d=%sn, result, argvresult); printf(3*n); printf(the order of command line parameters!n); for(result = 1; result argc; result+) printf(nat the end-argv%d=%sn, result, argvresult); printf(optopt = %dn,optopt); return 0;程序的执行结果 1:zan

15、sxubuntu-linux:/zanshixiang/myprogram$ ./a.out -a 123 -b 456 -c 7890*option=a, optopt= 0, optarg=123result is 97 argv3=-b optopt=0-option=b, optopt= 0, optarg=456result is 98 argv5=-c optopt=0-option=c, optopt= 0, optarg=789result is 99 argv7=(null) optopt=0-1*result = -1 optind = 72*the parameter a

16、fter optind:3*the order of command line parameters!at the end-argv1=-aat the end-argv2=123at the end-argv3=-bat the end-argv4=456at the end-argv5=-cat the end-argv6=789optopt = 0说明: 程序的最后,optind的数值为7,也就是命令行参数的最有一个参数的后面。 可以看出来,程序循环了三次,每次循环都正确的读取到了一个选项字符以及后面的参数。最后没有参数了,返回-1,循环正常结束,然后接着程序正常结束。程序的执行结果2:

17、zansxubuntu-linux:/zanshixiang/myprogram$ ./a.out -a 123 -e 1000*option=a, optopt= 0, optarg=123result is 97 argv3=-e optopt=0-option=?, optopt=e 101, optarg=(null)result is 63 argv4=100 optopt=101-e1*result = -1 optind = 42*the parameter after optind:-argv4=1003*the order of command line parameters

18、!at the end-argv1=-aat the end-argv2=123at the end-argv3=-eat the end-argv4=100optopt = 101说明: 这个时候在输入参数的时候,有-e这个非选项字符,这个时候函数返回 ?,optopt这个时候输出e ,optarg 是NULL,这个时候optind指向了100。程序的执行结果 3:zansxubuntu-linux:/zanshixiang/myprogram$ ./a.out -a 123 -e 100 -b 4560*option=a, optopt= 0, optarg=123result is 97

19、 argv3=-e optopt=0-option=?, optopt=e 101, optarg=(null)result is 63 argv4=100 optopt=101-eoption=b, optopt=e 101, optarg=456result is 98 argv7=(null) optopt=101-e1*result = -1 optind = 62*the parameter after optind:-argv6=1003*the order of command line parameters!at the end-argv1=-aat the end-argv2

20、=123at the end-argv3=-eat the end-argv4=-bat the end-argv5=456at the end-argv6=100optopt = 101说明: 这个时候在命令行参数的中间出现了不是选项的字符,这个时候“-a”,“ b”的参数都正确的读取,只有-e的时候出现了错误,而且optopt在读取-e之后就一直都是e,而且我前面说过的交换也出现了,命令行参数中“-e”后面的100被换到了最后,“-e”没有移动。程序的执行结果 4:zansxubuntu-linux:/zanshixiang/myprogram$ ./a.out -a 123 -e 100

21、 -b0*option=a, optopt= 0, optarg=123result is 97 argv3=-e optopt=0-option=?, optopt=e 101, optarg=(null)result is 63 argv4=100 optopt=101-eoption=?, optopt=b 98, optarg=(null)result is 63 argv6=(null) optopt=98-b1*result = -1 optind = 52*the parameter after optind:-argv5=1003*the order of command li

22、ne parameters!at the end-argv1=-aat the end-argv2=123at the end-argv3=-eat the end-argv4=-bat the end-argv5=100optopt = 98说明: 这个时候先看一下最后的结果,命令行参数的顺序变了,100跑到了“-e”的后面,没有仔细看前面的任这个时候就问了,既然数据交换到了“-e”的后面为什么在读-e参数的时候依然没有读到100,你不要像我当初忘了的那个静态的全局变get_data中成员_first_nonopt的作用。optopt先是指向了e 后来又换到了b,optind指向了100,也

23、就是最后争取读取选项字符的参数的后面。1-2 opstring中+,-,:的设置仅仅将上面的程序改动一条语句 while( (result = getopt(argc, argv, +c:a:b:) != -1 )程序的执行结果 1:zansxubuntu-linux:/zanshixiang/myprogram$ ./a.out -a 123 -b 456 -c 5670*option=a, optopt= 0, optarg=123result is 97 argv3=-b optopt=0-option=b, optopt= 0, optarg=456result is 98 argv5

24、=-c optopt=0-option=c, optopt= 0, optarg=567result is 99 argv7=(null) optopt=0-1*result = -1 optind = 72*the parameter after optind:3*the order of command line parameters!at the end-argv1=-aat the end-argv2=123at the end-argv3=-bat the end-argv4=456at the end-argv5=-cat the end-argv6=567optopt = 0说明

25、:正确的输入没有任何的问题。程序的执行结果 2:zansxubuntu-linux:/zanshixiang/myprogram$ ./a.out -a 123 -e 456 -c 5670*option=a, optopt= 0, optarg=123result is 97 argv3=-e optopt=0-option=?, optopt=e 101, optarg=(null)result is 63 argv4=456 optopt=101-e1*result = -1 optind = 42*the parameter after optind:-argv4=456-argv5=-c-argv6=5673*the order of command line p

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1