c语言+命令行参数
In this article, we’ll be looking at how we can use command line arguments in C / C++.
在本文中,我们将研究如何在C / C ++中使用命令行参数。
Command Line arguments are very useful if you want to pass any input strings to your main program, from the command line.
如果要从命令行将任何输入字符串传递到主程序,则命令行参数非常有用。
These arguments are passed as parameters to the main() function.
这些参数作为参数传递给main()函数。
Let’s look at how we can use these effectively.
让我们看看如何有效地使用它们。
为什么要使用命令行参数? (Why should we use command line arguments?)
Often, it is very convenient for us to directly give input to our program. One common way is to use scanf()
/ getchar()
, etc to wait for a user input.
通常,对于我们而言,直接向程序提供输入非常方便。 一种常见的方法是使用scanf()
/ getchar()
等来等待用户输入。
But, these calls waste a lot of time in waiting, and requires the user to manually enter the input.
但是,这些呼叫浪费大量时间等待,并且需要用户手动输入输入。
We can save a lot of time by simply giving these inputs to our main program!
通过将这些输入提供给我们的主程序,我们可以节省大量时间!
The format will be something like:
格式如下:
./executable input1 input2
The program will automatically store those command-line arguments in special variables, from which we can access them directly!
该程序将自动将这些命令行参数存储在特殊变量中,我们可以从中直接访问它们!
This will only require a one time input, given when we start our program.
启动程序时,只需输入一次即可。
Let’s look at how we can use them now.
让我们看看现在如何使用它们。
C / C ++中的命令行参数–特殊变量 (Command Line Arguments in C/C++ – The special variables)
The program will pass the command line arguments to the main()
function.
该程序会将命令行参数传递给main()
函数。
In C / C++, the main() function takes in two additional parameters for these arguments.
在C / C ++中,main()函数为这些参数引入了两个附加参数。
argc
-> Argument Count. Gives the number of arguments that we pass (includes the program name also)argc
>参数计数。 给出我们传递的参数数量(还包括程序名称)argv
-> Argument Vector. This is achar*
array of strings. These are the argument values itself.argv
>参数向量。 这是一个char*
字符串数组。 这些是参数值本身。
So, argv[0]
is the name of the program itself, and argv[1]
… argv[argc-1]
will be all our command line arguments.
因此, argv[0]
是程序本身的名称,而argv[1]
… argv[argc-1]
将是我们的所有命令行参数。
int main(int argc, char* argv[]);
To see this in action, let’s take an example.
为了了解这一点,让我们举个例子。
使用命令行参数–一个简单的例子 (Using command line arguments – A simple example)
Let’s consider a program which concatenates two strings, given as input.
让我们考虑一个程序,该程序将两个字符串作为输入给出。
We’ll pass in two command line arguments to our program, so our total argc
must be 3 (including the program name).
我们将两个命令行参数传递给我们的程序,因此我们的总argc
必须为3(包括程序名)。
We can write our program like this:
我们可以这样编写程序:
#include <iostream>
#include <string>
using namespace std;
string concat_strings(string s1, string s2) {
return s1 + s2;
}
int main(int argc, char* argv[])
{
cout << "You have entered " << argc
<< " arguments:" << "\n";
if (argc != 3) {
cerr << "Program is of the form: " << argv[0] << " <inp1> <inp2>\n";
return 1;
}
string result = concat_strings(argv[1], argv[2]);
cout << "Result: " << result << endl;
return 0;
}
If our executable name was test.out
, on my linux machine, I run the executable using this command:
如果我们的可执行文件名为test.out
,则在我的Linux机器上,我使用以下命令运行可执行文件:
./test.out Hello _JournalDev
Notice that the arguments are space-separated. So our command-line arguments are: “Hello” and “_JournalDev”
请注意,参数以空格分隔。 因此,我们的命令行参数为:“ Hello”和“ _JournalDev”
Output
输出量
You have entered 3 arguments:
Result: Hello_JournalDev
Great! This seems to work as expected, since the first argument is the name of the program itself.
大! 这似乎按预期工作,因为第一个参数是程序本身的名称。
Let’s try to run this with 4 arguments now.
现在让我们尝试使用4个参数运行它。
./test.out Hello from JournalDev
Output
输出量
You have entered 4 arguments:
Program is of the form: ./test.out <inp1> <inp2>
Indeed, it gives us the correct error message!
确实,它为我们提供了正确的错误消息!
结论 (Conclusion)
Hope this article gives you a better understanding of command line arguments. We saw how we can use it to make our lives easier!
希望本文能使您对命令行参数有更好的了解。 我们看到了如何使用它来使我们的生活更轻松!
For similar content, do go through our tutorial section on C++ programming.
对于类似的内容,请阅读我们有关C ++编程的教程部分 。
参考资料 (References)
- cplusplus.com post on Command Line arguments in C++ cplusplus.com关于C ++命令行参数的文章
翻译自: https://www.journaldev.com/41869/command-line-arguments-c-plus-plus
c语言+命令行参数