gpt4 book ai didi

c++ - 将 C++ .exe 与 C++ : C++ception 一起使用

转载 作者:行者123 更新时间:2023-11-28 07:17:31 25 4
gpt4 key购买 nike

<分区>

关于这个问题我需要一些帮助。我有一个 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
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com