【发布时间】:2013-11-15 16:39:55
【问题描述】:
在这个问题上我需要一些帮助。 我有一个 C++ .exe,我想用 C++ 打开它,然后在控制台中写入一些参数。
这是我想做的一个例子:
让我们假设一个带有此代码的可执行 whatsyourage.exe(实际上,我没有相应的代码):
#include <iostream>
using namespace std;
int main()
{
int age = 0;
cout << "What's your age ?" << endl;
cin >> age;
cout << "You are " << age << " year !" << endl;
return 0;
}
我想做类似的事情:
int main()
{std::string invit="21";
std::string chemin ="whatsyourage.exe";// in the same library
std::string const command = chemin+" "+ invit;
system(command.c_str());
}
我想写年龄(21)。
有人可以帮帮我吗?
答案如下:
int main()
{std::string invit="21";
std::string chemin ="whatsyourage.exe";
FILE* pipe = _popen(chemin.c_str(), "w");
if (pipe == NULL) {
perror("popen");
exit(1);
}
fputs("30", pipe);// write the age into the pipeline
pclose(pipe); // close the pipe
}
【问题讨论】:
标签: c++ executable