shantanu
shantanu

Reputation: 2418

Run program from c++ source which required multiple input

I need to run RNAeval (executable) from a c++ code and read the output of RNAeval. I found a code which can run a command and read the output.

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;
}

But RNAeval doesn't take any command line argument. Instead i need to provide input after run the program (similar to bc in linux).

example

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

How can i do this from c++?

System:

Linux
g++
gcc

Edit

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;

}

Upvotes: 1

Views: 1089

Answers (1)

HughB
HughB

Reputation: 373

popen returns a FILE object that you could use to write RNAEval's input stream. You can use fprintf to write commands to the process after you do the popen, then read in the results.

Upvotes: 1

Related Questions