I am working on a project that requires me to have output from within a mini shell on my C program to output to a file. Using ./program > file.txt
wi
You can use freopen.
freopen("desiredFileName", "w", stdout);
Once you are done, you can undo the reassignment like this:
freopen("/dev/stdout", "w", stdout);
on Unix/Linux systems. I'm not sure whether there is a portable way to revert back.
UPDATE
If possible, you can use fprintf
instead of printf
, providing either stdout
or a handle to the desired file, as appropriate.