C++ 从命令行输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11315854/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:02:06  来源:igfitidea点击:

Input from command line

c++command-line-arguments

提问by lovespeed

I have a program in C++ which I run for many values of a parameter. What I want to do is the following: Let's say I have two parameters as:

我有一个 C++ 程序,我为一个参数的许多值运行它。我想做的是以下内容:假设我有两个参数:

int main(){
    double a;
    double b;
//some more lines of codes
}

Now after after I compile I want to run it as

现在在我编译之后我想运行它

./output.out 2.2 5.4

So that atakes the value 2.2 and btakes the value 5.4.

所以这a取值为 2.2,b取值为 5.4。

Of course one way is to use cin>>but I cannot do that because I run the program on a cluster.

当然,一种方法是使用,cin>>但我不能这样做,因为我在集群上运行该程序。

回答by dasblinkenlight

You need to use command line argumentsin your main:

您需要在您的命令行 参数中使用main

int main(int argc, char* argv[]) {
    if (argc != 3) return -1;
    double a = atof(argv[1]);
    double b = atof(argv[2]);
    ...
    return 0;
}

This code parses parameters using atof; you could use stringstreaminstead.

此代码使用atof;解析参数 你可以stringstream改用。

回答by Ed S.

If you want to use command line parameters then no, you don'tuse cinas it is too late. You need to change your mainsignature to:

如果您想使用命令行参数,那么不,您不要使用cin,因为为时已晚。您需要将您的main签名更改为:

int main(int argc, char *argv[]) {
    // argc is the argument count
    // argv contains the arguments "2.2" and "5.4"
}

So you now have argvwhich is an array of pointer to char, each pointer pointing to an argument that was passed. The first argument is typically the path to your executable, subsequent arguments are whatever was passed in when your application was launched, in the form of pointers to char.

所以你现在有argvwhich 是一个指向 的指针数组char,每个指针指向一个传递的参数。第一个参数通常是可执行文件的路径,后续参数是应用程序启动时传入的任何内容,以指向char.

You will need to convert the char*'s to doubles in this case.

在这种情况下,您需要将char*'s转换为doubles。

回答by Richard J. Ross III

That's what command-line arguments are for:

这就是命令行参数的用途:

#include <sstream>

int main(int argc, char *argv[])
{
    if (argv < 3)
       // show error
    double a, b;

    std::string input = argv[1];
    std::stringstream ss = std::stringstream(input);

    ss >> a;

    input = argv[2];
    ss = std::stringstream(input);

    ss >> b;

    // a and b are now both successfully parsed in the application
}

回答by Zac

Have you looked at boost program options?

你看过boost程序选项吗?

It will take the command line arguments like many other are suggesting and let you provide a very consistent, clean and extensible command line interface.

它将采用许多其他建议的命令行参数,并让您提供非常一致、干净和可扩展的命令行界面。

回答by mathematician1975

You can use this form of the main()function to get command line arguments

您可以使用这种形式的main()函数来获取命令行参数

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

    } 

where the values of the argv[]array contain your command line variables as char*that you will need to convert to floatsor doublesin your case

其中argv[]数组的值包含您的命令行变量,char*因为您需要转换为floatsdoubles在您的情况下