Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about freopen() failure

Reply
Thread Tools

Question about freopen() failure

 
 
Kenneth Brody
Guest
Posts: n/a
 
      03-21-2008
What happens to the existing file stream if freopen() fails? For
example:

FILE *f = freopen("/a/filename/that/fails/to/open","w",stdout);

If the specified filename fails to open, is stdout now closed? From
my reading, it appears so:

7.19.5.4p4:

The freopen function first attempts to close any file that is
associated with the specified stream. Failure to close the file
is ignored.

In other words, the current stream is closed, and only then does it
attempt to open the specified file.

Am I correct that an freopen() on stdout that fails leaves the program
without any stdout, and that a future fopen() could end up giving that
file to stdout?

(A test program on a particular platform confirms that, on that
platform at least, this does occur.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-21-2008
Kenneth Brody wrote:
> What happens to the existing file stream if freopen() fails? For
> example:
>
> FILE *f = freopen("/a/filename/that/fails/to/open","w",stdout);
>
> If the specified filename fails to open, is stdout now closed? From
> my reading, it appears so:
>
> 7.19.5.4p4:
>
> The freopen function first attempts to close any file that is
> associated with the specified stream. Failure to close the file
> is ignored.


It seems to me that stdout "is closed" after the failed
freopen(), but that the original stream may or may not have
been "properly closed" -- for example, it may or may not
have been fflush()ed. It would certainly be a bad idea to
try to use stdout after the failure ...

You might want to try this question on comp.std.c.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
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
Success Or Failure: There Is No Such Thing As Failure bs866806@163.com C Programming 0 01-06-2008 11:41 AM
Question about "substitution failure is not an error" concept, SFINAE Aries Sun C++ 4 11-29-2007 12:54 PM
Run-Time Check Failure #n Question Antony C++ 8 02-09-2006 06:53 PM
[JAVA] [EVALUATION] - The Java Failure (Sorry: The Java(tm) Failure) Ilias Lazaridis Java 0 02-01-2005 10:32 AM
Re: 802.1x dhcp failure dpablo Wireless Networking 0 09-01-2004 12:03 AM



Advertisments