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

哪些公司可以做网站网站建设信息稿

哪些公司可以做网站,网站建设信息稿,淘宝详情页设计,秦皇岛网站制作公司哪家好getopt函数、optind等变量使用详解 最近在学习《Unix网络编程》vol2时#xff0c;发现书中例子经常使用一个命令行解析getopt函数#xff0c;因为函数声明比较特别#xff0c;根据自己摸索#xff0c;遂总结出使用方法。 1. getopt函数的声明 该函数是由Unix标准库提供的…getopt函数、optind等变量使用详解 最近在学习《Unix网络编程》vol2时发现书中例子经常使用一个命令行解析getopt函数因为函数声明比较特别根据自己摸索遂总结出使用方法。 1. getopt函数的声明 该函数是由Unix标准库提供的函数查看命令man 3 getopt #include unistd.hint 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 optindargv的当前索引值。当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.hint 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语句中将flags1tfnd1因为optarg变量保存这当前选项参数的字符串因此此时optarg保存的是‘-t’参数的参数并用atoi函数转称整数因此nsecs123。 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.w-s-a.com/news/295487/

相关文章:

  • 北京网站建设求职简历十堰seo优化教程
  • 网站顶部可关闭广告微信小程序多少钱
  • 网站背景怎么弄斜杠青年seo工作室
  • ps个人网站首页怎么制作如何做网站的版块规划
  • 做网站的市场开源建站工具
  • 邹平做网站哪家好自动点击器app
  • 南阳seo网站排名优化wordpress文章对游客不显示
  • 网站301什么意思湛江市seo网站设计报价
  • 免费建造网站化妆品网络营销方案
  • 建公司网站wordpress the content
  • 网站的站点的管理系统建设银行网站注册企业
  • 长春火车站是哪个站做微商哪个网站有客源
  • 亚马逊培训费用一般多少seo专业培训课程
  • 做推文封面图网站南宁高端网站建设
  • 天津网站搜索排名做电影免费ppt模板下载网站
  • 襄樊最好网站建设价格网站建设与设计 毕业设计
  • 网站推广广告词大全集网站建设相对路径
  • 甘肃省铁路投资建设集团有限公司网站域名怎么实名认证
  • 企业网站建设的层次返利网站建设
  • 竞价单页网站制作中小企业网站建设问题
  • 响应式网站源码学校网站制作多少钱
  • 营销型网站建设需要懂什么网站建站四件套是什么
  • 廊坊哪里有制作手机网站的区块链开发语言
  • 找建设项目的网站装企工长网站开发
  • 戴尔网站建设成功方正网站制作
  • 怎么买网站域名wordpress 视频站模版
  • 厦门官网建设公司杨和关键词优化
  • 怎么做网约车seo自动优化软件下载
  • 遵义市住房和城乡建设局官方网站网站备案 自己的服务器
  • 分销系统价格多少北京网站优化平台