By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

system, redirecting cout

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
m_*********@hotmail.com wrote:
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #2

P: n/a
Dietmar Kuehl is absolutly right.
If you are interested in POSIX interprocess communications to slove
your problem, you can found some examples developed in pure C language
here:

http://www.advancedlinuxprogramming....apter-5/pipe.c
http://www.advancedlinuxprogramming....apter-5/dup2.c

It will work with C++ io streams too.
Good luck.

Jul 23 '05 #3

P: n/a
Dietmar Kuehl wrote:
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*.
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.


Jul 23 '05 #4

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

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.