473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

system, redirecting cout

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
4 2026
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
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
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
Thanks everybody, with your hints I could solved the issue
(and dup2 was indeed crucial).
marc

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jerry Sievers | last post by:
Hello. Maybe I'm just out of practice, maybe not. Suppose we run a command using system() and this command writes to the stderr stream. If I remember correctly, this is normally written to the...
2
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My issue is that I redirect all of the output from this...
9
by: Michael | last post by:
I've written my progrma to use cout a lot, but now i want to output this to a file instead of the screen. How do i do this Is it platform specific (if so then sorry) Thanks Mike
3
by: Pawel Kruczynski | last post by:
did any know how redirect standard cout to edit in dialog window? i work in ms vc++ and i don't want use mfc. please help.. sorry for my poor english
2
by: Agoston Bejo | last post by:
Hi, how can one redirect std::cerr into a file? Specifically, if I have an ofstream object, is there something I can do to make "cerr << ..." calls write into that particular ofstream object? ...
0
by: Stephane | last post by:
hi, I'm using a 3rd party library producing output to stdout. As I'm developpong a console application, it is really annoying. I found a way to bypass it: //saving the old stdout stream...
1
by: Gurikar | last post by:
How to redirect information to file usinf cerr or clog. Regards
2
by: Jason Heyes | last post by:
Here is what I use to redirect printed messages to a file: std::ofstream file("logfile"); std::cout.rdbuf(file.rdbuf()); std::cout << "this message goes to a file" << std::endl; Will the...
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.