【发布时间】:2014-02-24 14:11:16
【问题描述】:
我需要从 c++ 代码运行 RNAeval(可执行文件)并读取 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;
}
【问题讨论】:
-
您可以使用 echo "command\ncommand2" |如果你使用 *nix 系统,程序作为你的参数。
-
它不起作用(input1\input2)。它不需要命令行参数。
标签: c++ interactive