0

I currently have two C++ programs. Program1 takes some input and prints to screen a float.

I can get Program2 to call Program1 using something like:
system("./program1 input");

Currently, Program1's output goes to screen. Is it possible for Program2 to read Program1's output and use it?

The only way I can think to do this is to have Program1 print to a file and then have Program2 read the file. However, this seems like an inelegant solution. Can somebody recommend a better way of accomplishing my objective.

Thanks in advance.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
1

That's called redirecting stdout to a file. Output to the screen is done with either stdout or stderr (in both C and C++).

Another way to do it is for program 2 to create a pipe through which program 1's stdout is redirected through the pipe to program 2. Here is an example in C language. I don't know if it can be used with c++ fstream class or not because I've never tried it.

Edited by Ancient Dragon: n/a

Votes + Comments
Thanks for the example, learn something keep up good work.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.