1

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;

}
Community
  • 1
  • 1
shantanu
  • 2,408
  • 2
  • 26
  • 56

1 Answers1

1

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.

HughB
  • 363
  • 2
  • 12
  • It works, But another problem. Now program doesn't terminate. result returns correctly, but doesn't terminate. When i use "r" flag it terminate successfully. – shantanu Jan 31 '14 at 20:34
  • It looks like you can only read OR write using popen. No read and write. Take a look at this http://stackoverflow.com/questions/6171552/popen-simultaneous-read-and-write, that may have a solution for you. – HughB Jan 31 '14 at 21:10