0

I haven't found any satisfactory answer so excuse me if this has already been answered before.

Basically I'd like to be able to call a program and inject some data in its stdin and read it's stdout from within a C program.

I'd like to emulate this:

cat data.txt | program

where the output of program is captured and the data is written to stdin of said program.

It's a bit like this question but in C instead of python and I don't need the subprocess to live forever, just execute with input data, spit out the output and die.

Repeatedly write to STDIN and read STDOUT of a Subprocess without closing it

Community
  • 1
  • 1
David
  • 3,324
  • 2
  • 27
  • 31
  • How do you start the subprocess? – Amin Negm-Awad Aug 03 '16 at 14:57
  • I don't know, I was expecting that to be part of the answer (popen, fork, etc.) – David Aug 03 '16 at 14:59
  • Possible duplicate of [popen() alternative](http://stackoverflow.com/questions/6743771/popen-alternative) – Amin Negm-Awad Aug 03 '16 at 15:13
  • The first answer contains an example of creating a subprocess with `popen()`/`exec…()` and using a pipe to communicate. As side note: Your Q sounds a bit if you have data in advance and want to get back data after the process finished. In such a case simply write the input data to a file, start the subprocess from that file and let him write out to a file and finally read the file after the process is done. Maybe this is easier in your scenario. – Amin Negm-Awad Aug 03 '16 at 15:15
  • Of course, `fork()`, not `popen()`. – Amin Negm-Awad Aug 03 '16 at 17:35

0 Answers0