You can use output redirection for this. It works perfectly in command line, and should work good in a program as well.
$ program > outputfile
This will record the output of the program
to the file outputfile
. You can later read this using normal fopen
command. (The dollar is the linux prompt.)