0

Hi,

I have to call an external executable program (prog2) from within my program (prog1) and then use the output of that exceutable program (prog2) in program (prog1) after calling it.

I have to manipulate the output obtained from progam (prog2) in program (prog1) and then perform this iteration n times.

I do not how to do this. Please let me know.

Thanks

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by guest7
1
/* ************************************* */
/* Function To Run A Shell Command, And  */
/* Read The Output Back Into Our Program */
/* ************************************* */
std::string run_command(string cmd)
{
        string data;
        FILE *stream;
        char buffer[1024];

        // Open The Command With Read Flag  
        stream = popen(cmd.c_str(), "r");
        while (fgets(buffer, 1024, stream) != NULL) {
                data.append(buffer);
        }
        pclose(stream);

        return data;
}

Usage Case:

// Find Running SSH Daemons In Linux
std::string grepvals = run_command("ps aux | grep \"sshd\"");
0

Hi,

Thanks for the sample code. If possible could you also refer me a book or article online where i can read more about it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.