Hi,
freopen closes stdout, but it seems that fclose doesn't restore it (see sample below).
How can one print to stdout after using freopen?
========= C code : File foo.c : BEGIN =========
#include <stdio.h>
#include <assert.h>
#define FILE_NAME "foobar"
int main ()
{
FILE* out_fp;
int rc;
remove (FILE_NAME);
printf ( "printf-1\n");
out_fp = freopen (FILE_NAME, "a", stdout);
assert (out_fp);
printf ("printf-2\n");
fprintf (out_fp, "fprintf(out_fp)\n");
rc = fclose (out_fp);
assert (!rc);
printf ("printf-3\n"); // Problematic printf
return 0;
}
========= C code : File foo.c : END ===========
========= Compilation & Run : BEGIN =========
$ gcc -v
[---omitted---]
gcc version 3.3.1 (cygming special)
$ gcc -W -Wall foo.c
$ a
printf-1
// printf-3 missing
$ cat foobar
printf-2
fprintf(out_fp)
========= Compilation & Run : END ===========
=====================================
Alex Vinokur
mailto:al****@connect.to
http://up.to/alexvn
=====================================