Q: How can I redirect stdin or stdout to a file from within a program?
A: 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();See, however, question 12.34.
References:
ISO Sec. 7.9.5.4
H&S Sec. 15.2