getopt()函数介绍
来源:互联网 发布:mac ui设计软件 编辑:程序博客网 时间:2024/05/17 04:40
getopt()(分析命令行参数)
表头文件 #i nclude<unistd.h>
定义函数 int getopt(int argc,char * const argv[ ],const char * optstring);
函数说明 getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
返回值 如果找到符合的参数则返回此参数字母,如果参数不包含在参数optstring 的选项字母则返回“?”字符,分析结束则返回-1。
范例 #i nclude<stdio.h>
#i nclude<unistd.h>
int main(int argc,char **argv)
{
int ch;
opterr = 0;
while((ch = getopt(argc,argv,”a:bcde”))!= -1)
switch(ch)
{
case ‘a’:
printf(“option a:’%s’/n”,optarg);
break;
case ‘b’:
printf(“option b :b/n”);
break;
default:
printf(“other option :%c/n”,ch);
}
printf(“optopt +%c/n”,optopt);
}
执行 $./getopt –b
option b:b
$./getopt –c
other option:c
$./getopt –a
other option :?
$./getopt –a12345
option a:’12345’
表头文件 #i nclude<unistd.h>
定义函数 int getopt(int argc,char * const argv[ ],const char * optstring);
函数说明 getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
返回值 如果找到符合的参数则返回此参数字母,如果参数不包含在参数optstring 的选项字母则返回“?”字符,分析结束则返回-1。
范例 #i nclude<stdio.h>
#i nclude<unistd.h>
int main(int argc,char **argv)
{
int ch;
opterr = 0;
while((ch = getopt(argc,argv,”a:bcde”))!= -1)
switch(ch)
{
case ‘a’:
printf(“option a:’%s’/n”,optarg);
break;
case ‘b’:
printf(“option b :b/n”);
break;
default:
printf(“other option :%c/n”,ch);
}
printf(“optopt +%c/n”,optopt);
}
执行 $./getopt –b
option b:b
$./getopt –c
other option:c
$./getopt –a
other option :?
$./getopt –a12345
option a:’12345’
- getopt()函数介绍
- [Linux]getopt() 函数介绍
- [Linux]getopt() 函数介绍 引用
- getopt函数
- getopt()函数
- getopt函数
- getopt函数
- getopt函数
- getopt函数
- getopt()函数
- getopt函数
- getopt函数
- getopt函数
- getopt函数
- getopt函数
- getopt函数
- getopt函数
- getopt函数
- APMServ 5.2.0 服务器平台搭建工具
- 07年12月29(晚上)
- 2007年的流水账
- 俺不是程序员
- javascript 显示时间
- getopt()函数介绍
- 操作系统(六)
- Apache (httpd)服务器防DDOS模块mod_evasive
- 在AIX下安装emacs23
- 在Eclipse中使用Ant来自动编译J2me程序
- ZOJ-1067-Color Me Less
- 搭建开发平台Eclipse
- 指针数组
- ProMes.Net中的 WikiFormatter.cs 文件中的脚本错误