Interview Questions

How can I redirect stdin or stdout to a file from within a program?

C Interview Questions and Answers


(Continued from previous question...)

How can I redirect stdin or stdout to a file from within a program?

Use freopen. If you're calling a function f() which writes to stdout, and you want to send its output to a file, and you don't have the option of rewriting f, you can use a sequence like:
freopen(file, "w", stdout);
f();

(Continued on next question...)

Other Interview Questions