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;

}
2
  • You can do that using echo "command\ncommand2" | program as your param, if you are using *nix systems. Commented Jan 31, 2014 at 18:39
  • It's doesn't work (input1\input2). It's doesn't take command line argument. Commented Jan 31, 2014 at 19:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
It looks like you can only read OR write using popen. No read and write. Take a look at this stackoverflow.com/questions/6171552/…, that may have a solution for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.