By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

freopen, fclose and stdout

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.