gpt4 book ai didi

c++ - 从需要多个输入的 C++ 源代码运行程序

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:49 24 4
gpt4 key购买 nike

我需要运行 RNAeval (可执行)来自 c++ 代码并读取 RNAeval 的输出。我找到了一个可以运行命令并读取输出的代码。

string exec(char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}

但是RNAeval不接受任何命令行参数。相反,我需要在运行程序后提供输入(类似于 linux 中的 bc)。

例子

RNAeval [enter]
input1 [enter]
input2 [enter]
return output by RNAeval and exit

我如何从 C++ 执行此操作?

系统:

Linux
g++
gcc

编辑

string exec(char* cmd) {
FILE* pipe = popen(cmd, "w");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
fprintf(pipe,"%s\n","acgt");
fprintf(pipe,"%s\n","(())");
fprintf(pipe,"%s\n","@");
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;

}

最佳答案

popen 返回一个 FILE 对象,您可以使用它来编写 RNAEval 的输入流。执行 popen 后,您可以使用 fprintf 向进程写入命令,然后读入结果。

关于c++ - 从需要多个输入的 C++ 源代码运行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21487582/

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