简体   繁体   English

是否可以在命令行中将输入参数传递给可执行文件

[英]Is it possible to pass input arguments to an executable in the command line

let's say I have this c++ code which I compiled into an executable out.exe假设我有这个 C++ 代码,我将它编译成一个可执行文件 out.exe

int main(){   
   int a = 0;  
   cin>>a;

   if (a)    
      cout<<"done";  

   return 0;  
}

Normally I would execute it using the command line by typing its name, then it would wait for my input.通常我会使用命令行输入它的名字来执行它,然后它会等待我的输入。

However, what I want it to pass the input in the same line that I am calling the executable like so:但是,我希望它在我调用可执行文件的同一行中传递输入,如下所示:

out.exe 1

1 being the input so the program wouldn't wait for my input and directly outputs: 1 是输入,因此程序不会等待我的输入并直接输出:

done

You can use int main(int argc, char **argv) command line arguments **argv , and argument counter argc look in What does int argc, char *argv[] mean?您可以使用int main(int argc, char **argv)命令行参数**argv和参数计数器argc看看int argc, char *argv[] 是什么意思? . .

The arguments are read as strings but you can easily convert them into the type you need, in this case int .参数被读取为字符串,但您可以轻松地将它们转换为您需要的类型,在本例中为int How can I convert a std::string to int? 如何将 std::string 转换为 int?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM