I’m using freopen for a GUI application using my shared library such that I can redirect debug msgs from stdout to a file
I have been using code based on a snippet from the C++ reference site:
/* freopen example: redirecting stdout */
#include <stdio.h>
int main ()
{
freopen ("myfile.txt","w",stdout);
printf ("This sentence is redirected to a file.");
fclose (stdout);
return 0;
}
This redirects stdout to myfile.txt and finally closes stdout
Is there a way to redirect to a file then effectively remove the redirection such that stdout then prints to screen as usual rather than closing it with fclose?
No, not in pure C. If you can assume a specific system (e.g. POSIX) there probably are options.
But frankly,
freopenis IMO a hack, only needed to be compatible with prewritten code. If you’re writing new code you should instead pass theFILE *to the relevant functions and usefprintfinstead ofprintf.