I’ve been using freopen (from stdio.h) function before without asking myself this question. But now I’m unsure.
E.g. I’ve reopened stdout:
#define OUTPUT_FILE "out.txt"
if ( freopen(OUTPUT_FILE,"w+",stdout) == NULL)
printf("logout can't be opened\n");
Usually every process has and handles stdout, stderr, stdin automatically and we needn’t care about closing of them.
But how is it to be here? I have reopened stdout.
Should I call fclose for closing reopened stdout?
PS
I’d be gladder to look at part of code that does this handling than to hear that I can be just confident that everything is fine here.
Thanks in advance, for any tips.
fclose(stdout);works to close the redirect.It is usually cleaner to close the filehandle.
If a filepointer is open when a program exits,
it will get closed for you.
But, if you just close the pointer,
stdout will not get redirected to the terminal again.
You can restore stdout to the terminal back again (and break redirects from command-line) with:
You can use
dup( example ) to restore the previous pointer or don’t use freopen at all, if you want to undo freopen ( http://c-faq.com/stdio/undofreopen.html ).