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!

1
  • Search for "pipe", "popen" or "redirection". Commented Dec 21, 2014 at 16:00

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! It looks realy easy to use
@marcopolo it opens file in writing mode.
@marcopolo I have provided link for freopen reference, and my video tutorial too.
Yea, I accpeted. When I saw it it was too soon to accept
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.