小编Ste*_*010的帖子

使用命令行参数执行另一个程序的C++程序

如何使用c ++程序中的参数执行命令行程序?这是我在网上找到的:

http://www.cplusplus.com/forum/general/15794/

std::stringstream stream;
stream <<"program.exe "<<cusip;
system(stream.str().c_str());
Run Code Online (Sandbox Code Playgroud)

但它似乎不接受实际的程序位置,所以我不知道如何应用它.我希望有这样的事情:

std::stringstream stream;
stream <<"C:\Tests\SO Question\bin\Release\HelloWorld.exe "<<"myargument";
system(stream.str().c_str());
Run Code Online (Sandbox Code Playgroud)

这给出了与反斜杠相关的几个警告 - 程序不起作用.是否期望您将程序放在某个特定位置?

这是我在控制台中获得的输出:

"C:\ Tests"不被识别为内部或外部命令,可操作程序或批处理文件.

附录:

所以根据Jon的回答,正确的版本对我来说是这样的:

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <cstring>
int main(int argc, char *argv[])
{

std::stringstream stream;    
stream << "\"C:\\Tests\\SO Question\\bin\\Release\\HelloWorld.exe\""
       << " " // don't forget a space between the path and the arguments
       << "myargument";
system(stream.str().c_str());

return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ command-line command command-line-arguments

7
推荐指数
2
解决办法
2万
查看次数