社区
Linux/Unix社区
帖子详情
为什么我的optind总是大于argc?
liken218
2003-10-20 04:36:31
我写了个命令,命令的格式是test <file>
在main里我取到的optind为什么总是argc?
能给我讲讲optind和argc吗?
...全文
442
4
打赏
收藏
为什么我的optind总是大于argc?
我写了个命令,命令的格式是test 在main里我取到的optind为什么总是argc? 能给我讲讲optind和argc吗?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
liken218
2003-10-20
打赏
举报
回复
如果是那就怪了,在内核中iptables-save.c的代码如下
int main(int argc, char *argv[])
{
const char *tablename = NULL;
int c;
program_name = "iptables-save";
program_version = NETFILTER_VERSION;
#ifdef NO_SHARED_LIBS
init_extensions();
#endif
while ((c = getopt_long(argc, argv, "bc", options, NULL)) != -1) {
switch (c) {
case 'b':
binary = 1;
break;
case 'c':
counters = 1;
break;
case 't':
/* Select specific table. */
tablename = optarg;
break;
case 'd':
do_output(tablename);
exit(0);
}
}
if (optind < argc) {
fprintf(stderr, "Unknown arguments found on commandline");
exit(1);
}
return !do_output(tablename);
}
就是说这个指令执行的时候,只要它后面加上选项则在if (optind < argc) 处就一定执行出错!
liken218
2003-10-20
打赏
举报
回复
那如果你的指令支持选项option
如 ./a.out -t myfile
则输出应该是
argc=3 optind=2
是吗?
elife
2003-10-20
打赏
举报
回复
你是不是这点弄错了: argv[0]是程序路径, 因此test <file>
argc=2 optind=1
elife
2003-10-20
打赏
举报
回复
optind
Name
optind -- external variable used in getopt()
Synopsis
extern int optind;
Description
optind holds the current index of the array argr[], which contains the command line options being parsed by getopt.
我的测试程序是对的啊
extern int optind;
int main(int argc)
{
int x = 10, y;
printf("argc=%d optind=%d\n", argc, optind);
}
./a.out 1 2
argc=3 optind=1
getopt函数的使用——分析命令行参数
getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int getopt(int
argc
,char * const argv[ ],const char * optstring); 全局变量 e...
C++ 常用&生僻函数小记
string类 find函数 substr函数 动态载入DLL相关函数 LoadLibrary函数 GetProcAddress函数 FreeLibrary函数 getopt函数 判断文件的访问权限 localtime函数 SetConsoleCtrlHandler控制台处理函数 inline函数 CreateDirectory创建文件目录 整形字符转换 strtoul函数 _ultoa函数 Set
System V 消息队列
System V消息队列使用消息队列标识符标识。跟Posix消息队列一样,进程在往消息队列写入消息之前,不要求另外某个进程在该队列上等待一个消息的到达。 消息队列的结构: #include
Linux getopt和getopt_long函数
getopt函数参数函数原型为: int getopt(int
argc
, char * const argv[], const char *optstring);涉及的全局变量:extern char *optarg; extern int
optin
d, opterr, optopt;包含的头文件:#include
先说几个概念: - 选项:指的是命令行参数中的-符号后面的
Webbench学习笔记二:getopt_long函数和build_request函数
程序中调用如下: while((opt=getopt_long(
argc
,argv,"912Vfrt:p:c:?h",long_options,&options_index))!=EOF ) 这句话的意思是检索参数选项。分为三个部分讲解。 先说几个参数:
argc
:参数个数 argv:参数内容 “912Vfrt:p:c:?h”:程序支持的命令行短选项。也就是命令行中支持-9、-1、-2、-V、-f、-r、-t、-p、-c、-?、-h十一个短选项,其中t、p、c三个字符后面有一个冒号,表示t、p、
Linux/Unix社区
23,110
社区成员
74,506
社区内容
发帖
与我相关
我的任务
Linux/Unix社区
Linux/Unix社区 应用程序开发区
复制链接
扫一扫
分享
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章