当前位置: 首页 > news >正文

合肥企业网站建设公司青岛网站制作排名

合肥企业网站建设公司,青岛网站制作排名,南宁网站建设加王道下拉,网上做问卷调查赚钱哪些网站好getopt函数、optind等变量使用详解 最近在学习《Unix网络编程》vol2时,发现书中例子经常使用一个命令行解析getopt函数,因为函数声明比较特别,根据自己摸索,遂总结出使用方法。 1. getopt函数的声明 该函数是由Unix标准库提供的…

getopt函数、optind等变量使用详解

最近在学习《Unix网络编程》vol2时,发现书中例子经常使用一个命令行解析getopt函数,因为函数声明比较特别,根据自己摸索,遂总结出使用方法。

1. getopt函数的声明

该函数是由Unix标准库提供的函数,查看命令man 3 getopt

#include <unistd.h>int getopt(int argc, char * const argv[], const char *optstring);extern char *optarg;
extern int optind, opterr, optopt;

getopt函数的参数:

  • 参数argc和argv:通常是从main的参数直接传递而来,argc是参数的数量,argv是一个常量字符串数组的地址。
  • 参数optstring:一个包含正确选项字符的字符串,如果一个字符后面有冒号,那么这个选项在传递参数时就需要跟着一个参数。

外部变量:

  • char *optarg:如果有参数,则包含当前选项参数字符串
  • int optind:argv的当前索引值。当getopt函数在while循环中使用时,剩下的字符串为操作数,下标从optind到argc-1。
  • int opterr:这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
  • int optopt:当发现无效选项字符之时,getopt()函数或返回 \’ ? \’ 字符,或返回字符 \’ : \’ ,并且optopt包含了所发现的无效选项字符。

根据下面的程序理解这四个参数

2. getopt函数的使用

man 3 getopt给出了该函数的例子

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>int main(int argc, char *argv[])
{int flags = 0, opt;int nsecs = 0, tfnd = 0;while((opt = getopt(argc, argv, "nt:")) != -1) {switch (opt) {case 'n':flags = 1;break;case 't':nsecs = atoi(optarg);tfnd = 1;break;default:printf("optopt = %c\n", (char)optopt);printf("opterr = %d\n", opterr);fprintf(stderr, "usage: %s [-t nsecs] [-n] name\n", argv[0]);exit(EXIT_FAILURE);} }printf("flags = %d; tfnd = %d; nsecs = %d; optind = %d\n", flags, tfnd, nsecs, optind);printf("optind = %d\n", optind);printf("argc = %d\n", argc);
#if 1 if(optind >= argc) {fprintf(stderr, "Expected argument after options\n"); exit(1);}
#endifprintf("name argument = %s\n", argv[optind]);/* Other code omitted */return 0;
}
  • getopt函数的第三个字符串参数 “nt:”:在man page中这样解释:-n, with no associated value; and -t val, which expects an associated value。中文意思为:-n 不用一个关联值;-t 需要有一个关联的value参数

运行结果:

➜  test ./a.out -a  name    //首先 -a 是一个非法的选项
./a.out: invalid option -- 'a'  //输出错误信息,因为非法选项
optind = 2  //此时argv[optind]是我们的操作数,也就是我们传递给主函数的参数
optopt = a  //当发现无效项字符时,optopt会包含该字符,正如我们传递的‘a’这个无效项。
opterr = 1  //opterr变量非零,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
usage: ./a.out [-t nsecs] [-n] name

再次运行:

➜  test ./a.out -t        //根据getopt函数的第三个参数,因为‘t’字符后有‘:’冒号,因此在‘-t’选项后需要跟随一个参数。因此此次运行的错误为“缺少参数”,如下提示:
./a.out: option requires an argument -- 't' //输出了错误信息,因为“缺少参数”
optind = 2  //argv[optind]为空,因为此次运行没有传递参数
optopt = t  //当发现无效项字符时,optopt会包含该字符,正如我们传递的‘t’缺少参数的选项。
opterr = 1  //opterr变量非零,getopt()函数为“无效选项”和“缺少参数“选项,并输出其错误信息。
usage: ./a.out [-t nsecs] [-n] name

再次运行:

➜  test ./a.out -t 123 -n     //此时,根据字符串”-nt:“应该得知,-t 后应加参数,-n不用加
flags = 1; tfnd = 1; nsecs = 123; optind = 4    //在switch语句中将flags=1,tfnd=1,因为optarg变量保存这当前选项参数的字符串,因此此时optarg保存的是‘-t’参数的参数并用atoi函数转称整数,因此nsecs=123。
optind = 4  //argv[optind]为空,因为没有向主函数传递参数
argc = 4    //一共四个字符串
Expected argument after options //该程序希望我们至少传递一个参数,因此在if语句中退出。

再次运行:

➜  test ./a.out -t 123 -n hello //‘-t’后加参数,‘-n’不用加,向main函数传递参数叫hello
flags = 1; tfnd = 1; nsecs = 123; optind = 4
optind = 4  //argv[optind] = ”hello“
argc = 5    //五个字符串
name argument = hello   //打印参数name argument= hello

其实可以传递多个参数(如果需要,因为此函数只打印第一个参数),例如:

➜  test ./a.out -t 123 -n hello world hello C
optind = 3
optind = 4
flags = 1; tfnd = 1; nsecs = 123; optind = 4
optind = 4
argc = 8
name argument = hello   
http://www.yayakq.cn/news/150682/

相关文章:

  • 网站兼容哪些浏览器天津网站建设诺亚
  • 专业做物业网站的公司中关村标准化协会
  • 网站tag标签郑州区块链数字钱包网站开发多少钱
  • 中国网站建设公司排名wordpress 静态地址
  • 公司招聘一个网站建设来做推广免费大数据查询
  • 专业建站的网站临沂网站关键字优化
  • 网站建设贰金手指科捷6wordpress实现动态功能
  • 做微信充值网站著名的营销成功的案例
  • 青岛网站美工做网站不实名认证可以吗
  • 免费域名怎么做网站西安公司排名
  • 山西网站建设公司黄石网站建设维护
  • 做我女朋友的网站牛商网专注营销型网站建设
  • seo网站排名优化公司网站建设改革情况汇报
  • 东莞皮具网站建设网站建设功能需求方案
  • 建站平台最便宜网站后台排版工具
  • 贵阳双龙区建设局网站上海网站开发薪资
  • 科技网站欣赏网线制作方法及步骤
  • 和田做网站的联系电话江苏镇江十大外贸公司
  • 可以做超大海报的网站济宁网站建设 优化培训
  • 动态的网站大概多少钱网站备案百度站长提交
  • 湖南昌华建设集团网站怎么学做淘宝电商网站
  • 中山h5网站建设非法期货做网站
  • 家用机能否做网站服务器国内自助建站
  • 门户网站 用什么语言做网站赚谁的钱
  • 网站建设培训速成做实验用哪些国外网站
  • 中山模板建站软件app开发的价格清单
  • 珠海手机网站建设费用wordpress能做交互类网站
  • 在建设一个公司网站多少钱园区门户网站建设方案
  • 为什么教育网站做的都很烂运动鞋官方网站建设计划书
  • 如何建设网站的外接 以及在增加外接的时应当注意什么成都谷歌seo