C++ 如何将命令行参数传递给 ac 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17645447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass command line arguments to a c program
提问by
I've known how to write a program that acceptscommand line arguments ever since I learned to program. What I don't understand is how these parameters get their values. Hopefully I don't have these two mixed up but there is a difference between an argument and a parameter. An argumentis the value given to the function when it is called such as: foo( a, b, c); where a, b, and c are the values. A parameteris the values that are inside the function while is being called.
自从我学会编程以来,我就知道如何编写一个接受命令行参数的程序。我不明白的是这些参数如何获得它们的值。希望我没有将这两者混淆,但参数和参数之间存在差异。一个参数是给被调用时,如函数的值:FOO(A,B,C); 其中 a、b 和 c 是值。一个参数是在函数内部,同时被调用的值。
So my question is how does a person pass command line argumentsto a program? I understand how to read the arguments, that argc
is the number of arguments, argv
is a pointer to an array of strings containing the arguments, etc. etc. but I just don't know how to give those arguments a value..
所以我的问题是一个人如何将命令行参数传递给程序?我了解如何读取参数,即参数argc
的数量,argv
是指向包含参数等的字符串数组的指针,等等,但我只是不知道如何为这些参数赋值。
I'm looking for information for both C and C++. I'm sort of a novice at this.
我正在寻找 C 和 C++ 的信息。我是这方面的新手。
采纳答案by TGH
In a Windows environment you just pass them on the command line like so:
在 Windows 环境中,您只需在命令行上传递它们,如下所示:
myProgram.exe arg1 arg2 arg3
argv[1] contain arg1 etc
argv[1] 包含 arg1 等
The main function would be the following:
主要功能如下:
int main (int argc, char *argv[])
回答by Yu Hao
On *nix:
在 *nix 上:
$ ./my_prog arg1 arg2
On Windows command line:
在 Windows 命令行上:
C:\>my_prog.exe arg1 arg2
In both cases, given the main
is declared as:
在这两种情况下,假设main
声明为:
int main (int argc, char *argv[])
argc
will be an int
with a value of 3, argv[1] = "arg1"
, argv[2] = "arg2"
, additionally, argv[0]
will have the name of the program, my_prog
.
argc
将是int
一个值为 3 的 , argv[1] = "arg1"
, argv[2] = "arg2"
,另外argv[0]
还有程序的名称,my_prog
。
Command line arguments are normally separated by space, if you wish to pass an argument with a space, like hello world
, use a double quote:
命令行参数通常用空格分隔,如果您希望传递带有空格的参数,例如hello world
,请使用双引号:
$ ./my_prog "hello world"
回答by Nobilis
On *nix, there's a very nice utilitythat lets you parse command-line flags and arguments in a very straightforward way. There's a nice example of its use on the same page.
在 *nix 上,有一个非常好的实用程序,可以让您以非常简单的方式解析命令行标志和参数。在同一页面上有一个很好的使用示例。
You would then run your program and pass arguments to it in a very standardised way:
然后,您将运行您的程序并以非常标准化的方式将参数传递给它:
$ ./my_app -a -b -c argument1 argument2
$ ./my_app -a -b -c argument1 argument2
You can do without it and just parse them on your own but if you're aiming to make your app useful to other people it's definitely worth the effort of making it conforming.
你可以不用它,只需自己解析它们,但如果你的目标是让你的应用程序对其他人有用,那么让它符合标准绝对值得。
回答by Ankit singh kushwah
Just click on start menu and type cmd in search index...press enter ..now in cmd window type following command... "program_name arg1 arg2" (without quotes) and press enter key...and yeah its done! and
只需单击开始菜单并在搜索索引中键入 cmd...按 Enter ..now 在 cmd 窗口中键入以下命令...“program_name arg1 arg2”(不带引号)并按 Enter 键...是的,它完成了!和