本文介绍了从命令行输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个C ++程序,我运行的参数的许多值。我想做的是:
假设我有两个参数:
int main(){
double a;
double b;
//更多的代码行
}
我想运行它
./ output.out 2.2 5.4
pre>
因此
a
取值为2.2,b
取值5.4。
当然一个方法是使用
cin>>
,但我不能因为我在群集上运行程序。解决方案您需要使用;您可以改用。
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:
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
a
takes the value 2.2 andb
takes the value 5.4.Of course one way is to use
cin>>
but I cannot do that because I run the program on a cluster.解决方案You need to use command line arguments in your
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 usestringstream
instead.这篇关于从命令行输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!