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.

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.

commented: Thanks for the example, learn something keep up good work. +2
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.