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

Dani AI

Generated

A few practical points that build on 's pointer and 's example: there are three common approaches depending on how much control you need and how often you must call the other program.

For one-off or simple captures, a simple library call (the example above is one) is fine. For robust control (separate stdin/stdout/stderr, timeouts, nonblocking I/O, explicit exit codes) use the native OS APIs: on Windows use CreateProcess with redirected handles (CreateProcess docs); on POSIX use spawn/pipe/fork+exec or posix_spawn for portability (posix_spawn man page). For cross-platform convenience consider a maintained library such as Boost.Process ().

If you must iterate many times, avoid repeatedly spawning new processes if possible. Start a worker once and communicate over its stdin/stdout, or use a socket/FIFO/pipe protocol or a persistent child — that greatly reduces overhead and simplifies state passing. If the external program expects a TTY, use a pty wrapper.

Several cautions: never build shell command strings from untrusted input (risk of injection); prefer APIs that accept argv-style argument lists or a library that safely escapes arguments. Always read both stdout and stderr (or merge them) and check the child exit status. Watch for blocking reads and deadlocks — use nonblocking I/O, separate reader threads, or select/poll. Implement timeouts and ensure handles are closed to avoid resource leaks.

For further reading, follow the links above (CreateProcess, posix_spawn, Boost.Process) and the relevant man pages for your platform.

Recommended Answers

All 4 Replies

look into pipes

Chris

Hi,

Could you give a small example.

Thanks

/* ************************************* */
/* 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\"");

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.