C++程序执行另一个带有命令行参数的程序
C++ Program to execute another Program with Command line arguments
如何使用c++程序中的参数执行命令行程序?这就是我在网上发现的:
http://www.cplusplus.com/forum/general/15794/
std::stringstream stream;
stream <<"program.exe "<<cusip;
system(stream.str().c_str());
但它似乎不接受实际的程序位置,所以我不确定如何应用它。我希望有这样的东西:
std::stringstream stream;
stream <<"C:TestsSO QuestionbinReleaseHelloWorld.exe "<<"myargument";
system(stream.str().c_str());
这会给出几个与反斜杠相关的警告,并且程序无法运行。它希望你把这个程序放在某个特定的地方吗?
这是我在控制台中得到的输出:
"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;
}
首先,只要希望实际字符串值中出现一个反斜杠,就应该在文字字符串中使用双反斜杠。这是根据语言语法;一致性编译器可能比简单地警告更糟糕。
在任何情况下,您遇到的问题都是由于在Windows中包含空格的路径必须用双引号括起来。由于双引号本身需要在C++字符串文本中转义,因此需要编写
stream << ""C:\Tests\SO Question\bin\Release\HelloWorld.exe""
<< " " // don't forget a space between the path and the arguments
<< "myargument";
这给出了几个与反斜杠相关的警告
我相信是C++中的转义字符,使用
\
可能会解决这个问题。
相关文章:
- LLVM | codegen 用于带有命令行选项的程序输入功能
- 程序运行,但文件无法打开.C++中的命令行
- 编写一个将 LLVM IR 文件作为命令行参数的程序
- 使用参数 + stdout + stderr 从命令行调用 MFC 应用程序
- 如果调用 RtlSetProcessIsCritical,将使用 lstrcmpW 将命令行与值进行比较将使程序崩溃
- QProcess可以启动程序,但不能启动python(命令行)
- 命令行参数执行C 程序
- 如何在C++命令行界面程序中运行 COM (.ocx) 对象.(VS2017)
- 我可以通过命令行参数传递给C++程序多少数据?
- 使用命令行编译 gtkmm 程序
- 在控制台窗口关闭(在Windows上)上优雅地关闭命令行应用程序
- 硬币或CBC求解器性能:命令行实用程序与编译的C 程序
- 节点 js 我可以从用 C++ 编写的 windows 命令行应用程序中获取退出代码
- qt 命令行应用程序编译
- 使用提升的权限打开命令行程序
- Python:使用<cstdio>时与c ++命令行程序的通信不起作用
- 在命令行程序中使用布尔值。找不到定义标志并检查它们是否在参数中的方法
- 通过命令行编译和链接 Swift/Objective-C++ 应用程序
- 使用AWS-SDK-C 构建MacOS C 命令行应用程序
- Android本机应用程序命令行参数