1

I wrote a program that compiles and runs an external program(prog1.c). And I want to get the output of the prog1.c somehow.
for example if prog1.c is printf("HELLO"); and prints HELLO to the terminal, I want this output to get to a variable in my program or a file.
I saw somewhere something about pipe, but I dont know this command much and dont know if this is the right way.

Anyone can help me, direct me or show me an example?
Thank you!

marco polo
  • 159
  • 1
  • 2
  • 12

2 Answers2

1

Using freopen you can redirect your printf output to file.

freopen("c:\\output\\output.txt","w",stdout);
printf("write in file using printf"); //this will be printed to file, not to console

If you want to understand more you can see my video tutorial on this.

In this example I have not used pipe, but redirected standard output to file rather than console.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
0

you can also use pipe (and fork) to redirect outpout from prog1 to your input in the other prog, and check waitpid to catch return value too