çgetopt说明多重价值 [英] C getopt multiple value

查看:101
本文介绍了çgetopt说明多重价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的观点是这样的。

./a.out -i file1 file2 file3

我如何可以利用 getopt的()拿到3(或更多)输入文件?
我在做这样的事情:

How can I utilize getopt() to get 3 (or more) input files? I'm doing something like this:

while ((opt = getopt(argc, argv, "i:xyz.."))!= -1){
  case 'i':
     input = optarg; 
     break;
  ...
}

我得到的只是文件1 ;如何让文件2 文件3

推荐答案

如果你一定要,你可以在启动的argv [OPTIND] 和增量 OPTIND 自己。不过,我不推荐这个,因为我认为语法是表现欠佳。 (你怎么知道当你到达列表如果有人有一个名为文件的结束 - <?/ code>作为第一个字符)

If you must, you could start at argv[optind] and increment optind yourself. However, I would recommend against this since I consider that syntax to be poor form. (How would you know when you've reached the end of the list? What if someone has a file named with a - as the first character?)

我认为,这将是更好的为你的语法更改为:

I think that it would be better yet to change your syntax to either:

/a.out -i file1 -i file2 -i file3

或者治疗文件作为位置参数的列表:

Or to treat the list of files as positional parameters:

/a.out file1 file2 file3

这篇关于çgetopt说明多重价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆