system, redirecting cout

M

m_schellens

I want to spawn an external program from my C++ app.
And I want to get the standard and error output from
the spawned program.
Spawning is done in C++ with the

int system(const char *s)

function, but how do I redirect its output into lets say
a vector of strings which I can examine from within my program.

Thanks,
marc
 
D

Dietmar Kuehl

but how do I redirect its output into lets say
a vector of strings which I can examine from within my program.

Only with standard C++ facilities, you can't redirect the output
of called program directly: the only thing the standard C++
library supports is 'std::system()' which just calls the program
and returns the exit status. If you want redirection, you need
some interprocess communication stuff which is not covered by the
C++ standard.

On POSIX like machines you would use pipe(2), fork(2), some
variant of exec(2), and wait(2) to do redirect the input and or
the output of the spawned program. These operations are
encapsulated by some libraries, e.g. as a pipe stream. On other
systems you would use a similar approach but other system calls:
you need to ask for this in an appropriate environment specific
forum.
 
R

Rolf Magnus

Dietmar said:
On POSIX like machines you would use pipe(2), fork(2), some
variant of exec(2), and wait(2) to do redirect the input and or
the output of the spawned program.

You'd also need dup2(2).
These operations are encapsulated by some libraries, e.g. as a pipe
stream.

POSIX also has popen(3), which wraps it into a FILE*.
 
M

m_schellens

Thanks everybody, with your hints I could solved the issue
(and dup2 was indeed crucial).
marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top