Friday, 15 May 2015

C freopen descriptor - close manually/leave opened -



C freopen descriptor - close manually/leave opened -

i've been using freopen (from stdio.h) function before without asking myself question. 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 opened\n");

usually every process has , handles stdout, stderr, stdin automatically , needn't care closing of them. how here? have reopened stdout. should phone call fclose closing reopened stdout?

ps i'd gladder @ part of code handling hear can confident fine here. in advance, tips.

fclose(stdout); works close redirect.

it cleaner close filehandle. if filepointer open when programme exits, closed you.

but, if close pointer, stdout not redirected terminal again.

freopen("re.txt", "w", stdout); printf("this redirected stdout"); fclose(stdout); printf("this not");

you can restore stdout terminal 1 time again (and break redirects command-line) with:

freopen("/dev/tty", "a", stdout);

you can utilize dup ( example ) restore previous pointer or don't utilize freopen @ all, if want undo freopen ( http://c-faq.com/stdio/undofreopen.html ).

c file-descriptor stdio

No comments:

Post a Comment