473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 12571
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex Vinokur | last post by:
Hi, Where has output-to-cout gone in the program below? Thanks, ========= C++ code : File foo.cpp : BEGIN ========= #include <cstdio> #include <cassert> #include <iostream>
2
by: Steve | last post by:
Is it allowed to fclose stdin? Trying it here it seems to work ok, but it occurs to me that on some other system the standard library might call fclose(stdin) during its clean up, and calling...
3
by: John Devereux | last post by:
Hi, I am a bit confused about the behaviour of freopen() and the standard i/o streams. I am trying to redirect a stream (stderr) to a particular "file". But, if the call fails I want it to...
20
by: David Mathog | last post by:
A program of mine writes to a tape unit. Output can be either through stdout or through a file opened with fopen(). When all the data is transferred to tape the program needs to close the output...
7
by: Pietro Cerutti | last post by:
Hi group, I just noticed that a malloc w/out relative free occurs in the standard C library (or at least, on my implementation of it). #include <stdio.h> int main(void) { printf("%s\n",...
2
by: Guillaume Dargaud | last post by:
Hello all, a while ago I was pointed towards freopen as a way to redirect stderr to a log file. It works great, but apparently the app also writes a few lines to stdout. Now I could redirect to 2...
16
by: Bill Cunningham | last post by:
Is it really necessary to check the return type (int) or fclose ? fopen I can understand but what about using fflsuh(fp); /* fopen's pointer */ fclose(fp); Would that take care of any...
3
by: wrenashe | last post by:
I am on windows 2003, basically my codes want to do such a thing: 1. fopen a file A. 2. dup2(A, stdout); 3. dup2(A, stderr); 4. fclose(A); 5. rename(A), and move it to somewhere else. 6,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.