Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I take the output of one program and use it as the input of another on C++?

I have a program that takes the counts of experiments as command string argument and outputs the sequence of floating numbers. Example: im_7.exe 10 10.41 13.33 8.806 14.95 15.55 13.88 10.13 12.22 9.09 10.45

So, i need to call this program in my program and analyze this sequence of numbers.

like image 530
Mixabuben Avatar asked Dec 21 '22 13:12

Mixabuben


2 Answers

If your are on windows then you need to do the following

  1. Create a Pipe1 using CreatePipe api of windows. Use this pipe for reading data from STDOUT of the child process.
  2. Create a Pipe2 the same way and use that pipe for writing data to the STDIN of the child process.
  3. Create the child process and in the startup info provide these handles and inherit the handles from the parent process. Also pass the cmd line arguments.
  4. Close the write end of Pipe1 and read end of Pipe2.
  5. In your case you are not writing anything into the child process input. You can straight away read the data from the child process output by reading from the Pipe1.

For a sample have a look at the following link. http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx

Hope this is what you are looking for.

like image 60
ferosekhanj Avatar answered Dec 28 '22 08:12

ferosekhanj


Data that one program prints to standard output (std::cout in C++) can be piped to the standard input (std::cin) of another program. The specifics of how the two programs are connected depends on the environment (specifically operating system and shell).

like image 37
Oswald Avatar answered Dec 28 '22 07:12

Oswald