如何编写一个从命令行获取参数的程序? [英] how to write a program that takes arguments from commandline?

查看:109
本文介绍了如何编写一个从命令行获取参数的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在学习C编程。我有一个程序,我想要b $ b修改所以它需要来自命令行的参数。我们打电话给程序:

program.exe。


有人可以很快解释我是如何得到这种行为的:


C:>程序-help或C:>程序-h

printf(" \ nBla.bla。这是一些帮助和参数\ n")....等。


C:> program -dt = 0.1 -tend = 10(可能会添加其他开关)。

在程序中:


float(或double)dt应该变为0.1。名为

" tend的整数变量应分别指定为10.


如果出现任何问题,例如C:> program dt = adg,

程序应该回复像dt:语法无效之类的东西。退出。。


我怀疑应该更改程序,以便int main(??

这里有什么东西,对吗?) "而不是int main(void),但我不是

非常确定如何解决这个问题。如果有人在C中有任何样本

代码发布,我会非常高兴。


提前感谢任何提示......

Med venlig hilsen /最好的问候

Martin J?rgensen


-

------ -------------------------------------------------- -------------------

Martin J?rgensen的家 - http://www.martinjoergensen.dk

推荐答案

文章< j4 * ***********@news.tdc.dk>,

=?ISO-8859-1?Q?Martin_J = F8rgensen?=< un ***** ****@spam.jay.net>写道:
In article <j4************@news.tdc.dk>,
=?ISO-8859-1?Q?Martin_J=F8rgensen?= <un*********@spam.jay.net> wrote:
我正在学习C编程。我有一个程序,我想修改,所以它从命令行接受参数。
我怀疑应该改变程序,使得int main(??
某些东西进入这里,对吗?)而不是int main(void),但我不是很确定如何解决这个问题。
I''m learning C-programming. I have a program which I would like to
modify so it takes arguments from the commandline. I suspect that one should change the program such that "int main(??
something goes in here, right?)" instead of int main(void), but I''m not
really sure of how to address this problem.




嗯,我想不出任何介绍性的C文本没有

涵盖了传递参数的机制。


更难的部分是干净有效地解析传入的内容,

特别是如果某些选项修改了其他选项的含义或某些选项不能与其他选项共存。对于机制,请参阅大多数

任何程序并查找argc和argv。

-

law - it''sa商品

- Andrew Ryan(环球邮报,2005/11/26)



Ummm, I cannot think of any introductory C text which does not
cover the mechanics of passing in arguments.

The harder part is in cleanly and efficiently parsing what gets passed in,
especially if some options modify the meaning of others or some
options cannot co-exist with others. For the mechanics, see most
any program and look for argc and argv .
--
"law -- it''s a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)


你是对的,当你定义main,你这样做:

int main(int argc,char * argv [])

argc是指定的参数个数,program.exe算作

也是一个参数。

argv是给出的参数。它是一个常量数组,因此它从[0]

(即program.exe)开始。 [1]将是-h或-help,依此类推。我已经看到argv写成了char ** argv,而不是传统的char

* argv [],但我想这取决于你。


我遇到的一个问题是:在测试参数时,请说:

if(argv [1] ==" -help" ;)

在你的情况下,它不起作用。如果您尝试:

---

if(argv [1] ==" -help")

printf(" \\ \\ n< help>");

else

printf(" \\\
you typed%s",argv [1]);

---

然后无论如何,它都会落入其他声明。

You''re right, when you define main, you do:
int main(int argc, char *argv[])
argc is the number of arguments specified, program.exe would count as
an argument, too.
argv is the arguments given. It''s a constant array, so it starts at [0]
(that would be program.exe). [1] would be -h or -help, and so on. I''ve
seen argv written as char **argv rather than the conventional char
*argv[], but I guess it''s down to you.

One problem I''ve run into is this: when testing for an argument, say:
if(argv[1] == "-help")
in your case, it doesn''t work. If you try:
---
if(argv[1] == "-help")
printf("\n<help>");
else
printf("\nyou typed %s", argv[1]);
---
then no matter what, it drops to the else statment.


" Martin J?rgensen" ; <未********* @ spam.jay.net>在消息中写道

news:j4 ************ @ news.tdc.dk ...
"Martin J?rgensen" <un*********@spam.jay.net> wrote in message
news:j4************@news.tdc.dk...

<我正在学习C编程。我有一个程序,我想修改,所以它从命令行接受参数。我们打电话给程序:
program.exe。

有人可以很快解释我是如何得到这种行为的:

C:> program -help或C:> ;程序-h
printf(" \ nBla.bla。这里有一些帮助和参数\ n")....等等。

C:> program -dt = 0.1 -tend = 10(可能会增加额外的开关)。
在程序中:

浮动(或双倍)dt应该变为0.1。名为
tend的整数变量应分别指定为10.

如果出现任何问题,例如C:> program dt = adg,
程序应该响应类似dt:无效语法的内容。退出。。

我怀疑应该更改程序,以便int main(??
某些东西进入这里,对吗?)而不是int main(void),但我不确定如何解决这个问题。如果有人在C中有任何样本代码发布,我会非常高兴。

提前感谢任何提示......
Hi,

I''m learning C-programming. I have a program which I would like to
modify so it takes arguments from the commandline. Let call the program:
program.exe.

Could somebody shortly explain how I get this behaviour:

C:>program -help or C:>program -h
printf("\nBla. bla. Here is some help and arguments\n").... etc.

C:>program -dt=0.1 -tend=10 (etc. additional switches might be added).
In the program:

float (or double) dt should become 0.1. The integer variable named
"tend" should be assigned respectively to 10.

In case of any problems such as for instance C:>program dt=adg, the
program should respond with something like "dt: Invalid syntax. Exiting.".

I suspect that one should change the program such that "int main(??
something goes in here, right?)" instead of int main(void), but I''m not
really sure of how to address this problem. If somebody has any sample
code in C to post, I would be very happy.

Thanks in advance for any hints...




也许这是相关的:
http:// c -faq.com/misc/argv.html


这篇关于如何编写一个从命令行获取参数的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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