DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   How to call an external exceutable program. (http://www.daniweb.com/forums/thread166014.html)

guest7 Jan 4th, 2009 2:02 pm
How to call an external exceutable program.
 
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

Freaky_Chris Jan 4th, 2009 2:07 pm
Re: How to call an external exceutable program.
 
look into pipes

Chris

guest7 Jan 4th, 2009 2:16 pm
Re: How to call an external exceutable program.
 
Hi,

Could you give a small example.

Thanks

Comatose Jan 4th, 2009 3:07 pm
Re: How to call an external exceutable program.
 
/* ************************************* */
/* 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\"");

guest7 Jan 4th, 2009 3:44 pm
Re: How to call an external exceutable program.
 
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.


All times are GMT -4. The time now is 8:42 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC