Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > freopen, fclose and stdout

Reply
Thread Tools

freopen, fclose and stdout

 
 
Alex Vinokur
Guest
Posts: n/a
 
      12-03-2003
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
(E-Mail Removed)
http://up.to/alexvn
=====================================





 
Reply With Quote
 
 
 
 
Grumble
Guest
Posts: n/a
 
      12-03-2003
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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-03-2003
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
schneidp20 schneidp20 is offline
Junior Member
Join Date: Jan 2009
Posts: 1
 
      01-24-2009
I couldn't take no for an answer and dug a little deeper. At least for MS systems you can switch back and forth from stdout and redirected stdout:

printf("To Console\n");
f = freopen("out.txt","w",stdout);
printf("redirect to file\n");
f = freopen("CON", "w", stdout);
printf("back to console\n");

Enjoy!
David Schneider
 
Reply With Quote
 
vidyaani vidyaani is offline
Junior Member
Join Date: Jan 2013
Posts: 1
 
      01-11-2013
Solution:

you can type these freopen ("/dev/tty", "a", stdout).
before that printf statement which you want to print in the stdout
Is this what you were trying to do
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: dup2, fclose and rename ... David Thompson C Programming 0 09-01-2008 06:53 AM
safe to fclose stdout to avoid memory leak? Pietro Cerutti C Programming 7 08-30-2007 09:37 PM
fclose then fopen equivalent for stdout? David Mathog C Programming 20 10-27-2006 03:54 PM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
fopen and fclose? kathy C++ 17 02-15-2006 06:14 PM



Advertisments