社区
Linux/Unix社区
帖子详情
windows下的getopt函数
rastevil
2007-08-30 04:37:22
请问,在WIN下要调用这个函数,要怎么做?
需要哪些库?
...全文
1286
4
打赏
收藏
windows下的getopt函数
请问,在WIN下要调用这个函数,要怎么做? 需要哪些库?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
dai_weitao
2007-08-30
打赏
举报
回复
补一句:
出自<Linux C函数手册>
core
2007-08-30
打赏
举报
回复
蛮多开源软件里有,比如apache
hzcpig
2007-08-30
打赏
举报
回复
getopt(分析命令行参数)
相关函数
表头文件 #include<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。
范例 #include<stdio.h>
#include<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’
dai_weitao
2007-08-30
打赏
举报
回复
楼主想问的是getsockopt吧?
windows
下的
getopt
/
getopt
_long等
函数
,vs2010实测可用
GNU libc提供了
getopt
和
getopt
_long用于解析命令行参数,使用方便,但是
windows
环境没有提供。故将GNU libc提供的源码稍加修改,整理出了
windows
下可用的
getopt
和
getopt
_long。
C++版本
Windows
命令行
getopt
参数类
功能:C++命令行参数解析类。(控制台应用程序中使用)在
Windows
平台下使用 Linux 的
getopt
,
getopt
_long,
getopt
_long_only
函数
。压缩包内包含
getopt
.h,
getopt
.c和使用代码示例。在VS2010下编译通过,运行良好。
getopt
windows
_src.zip
getopt
/
getopt
_long
函数
是GNU C中的
函数
,在linux编程中很常用到。这里就不介绍了。
windows
下没有找到类似的
函数
,于是乎从glibc中找出来修改为
windows
可用的命令行参数。 vs亲写可用
getopt
windows
实现
在
Windows
平台下使用 Linux 的
getopt
,
getopt
_long,
getopt
_long_only
函数
。
Windows
下完整实现了命令行
函数
分析,实测VS编译器非常好用!
Windows
下完整实现了命令行
函数
分析,实测VS编译器非常好用!
Linux/Unix社区
23,120
社区成员
74,507
社区内容
发帖
与我相关
我的任务
Linux/Unix社区
Linux/Unix社区 应用程序开发区
复制链接
扫一扫
分享
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章