469,313 Members | 2,559 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,313 developers. It's quick & easy.

freopen, fclose and stdout

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
=====================================

Nov 13 '05 #1
2 12266
Alex Vinokur wrote:
freopen closes stdout, but it seems that fclose doesn't restore it (see sample below).

How can one print to stdout after using freopen?


http://www.eskimo.com/~scs/C-faq/q12.34.html

Nov 13 '05 #2
Alex Vinokur wrote:

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?


This is Question 12.34 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... and you're not going to like the answer.

--
Er*********@sun.com
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Alex Vinokur | last post: by
2 posts views Thread by Steve | last post: by
3 posts views Thread by John Devereux | last post: by
20 posts views Thread by David Mathog | last post: by
7 posts views Thread by Pietro Cerutti | last post: by
2 posts views Thread by Guillaume Dargaud | last post: by
16 posts views Thread by Bill Cunningham | last post: by
3 posts views Thread by wrenashe | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.