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.
ISO Sec. 18.104.22.168
H&S Sec. 15.2