Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > fopen and fclose?

Reply
Thread Tools

fopen and fclose?

 
 
kathy
Guest
Posts: n/a
 
      02-03-2006
if fopen failed, does it necessary to call fclose?

I see an example like this:
....
stream = fopen(...);
if(stream == NULL)
{
....
}
else
{
....
}
fclose(stream);
....

By my understanding, it should like this:
....
stream = fopen(...);
if(stream == NULL)
{
....
}
else
{
....
fclose(stream);
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-03-2006
kathy wrote:
> if fopen failed, does it necessary to call fclose?


No. And actually I can't find any description of what's going to happen
if you pass a null pointer to 'fclose'. Conclusion: it's undefined
behaviour and should be avoided. IOW, you must _not_ call 'fclose' for
a pointer obtained from 'fopen' if opening failed (and null pointer is
returned).

> [..]


V
 
Reply With Quote
 
 
 
 
DraakUSA
Guest
Posts: n/a
 
      02-03-2006
It should be like this:

stream = fopen(...);
if (stream == NULL)
{
...
}
else
{
...
fclose(stream);
}

fclose'ing a NULL pointer is undefined and could cause problems.
fclose a stream ONLY iff it is a valid open stream.

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      02-03-2006
kathy wrote:
> if fopen failed, does it necessary to call fclose?


It doesn't hurt anything to call fclose with a null pointer, and the
function will return an error code if it fails to close the supplied
file.

>
> I see an example like this:
> ...
> stream = fopen(...);
> if(stream == NULL)
> {
> ...
> }
> else
> {
> ...
> }
> fclose(stream);
> ...
>
> By my understanding, it should like this:
> ...
> stream = fopen(...);
> if(stream == NULL)
> {
> ...
> }
> else
> {
> ...
> fclose(stream);
> }


Your way is preferable, IMHO, but I think either is legal. Better still
might be to use std::fstream instead.

Cheers! --M

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      02-03-2006
mlimber wrote:
> kathy wrote:
> > if fopen failed, does it necessary to call fclose?

>
> It doesn't hurt anything to call fclose with a null pointer


I stand corrected.

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      02-03-2006
mlimber wrote:
> mlimber wrote:
> > kathy wrote:
> > > if fopen failed, does it necessary to call fclose?

> >
> > It doesn't hurt anything to call fclose with a null pointer

>
> I stand corrected.


Hmm. On second... er, third thought, I'm not sure what the standard
mandates, but the IRIX 6.5 manpages do say: "For fclose, EOF is
returned if stream is NULL, or stream is not active, or there was an
error when flushing buffered writes, or there was an error closing the
underlying file descriptor."

Cheers! --M

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-04-2006
mlimber wrote:


> Hmm. On second... er, third thought, I'm not sure what the standard
> mandates, but the IRIX 6.5 manpages do say: "For fclose, EOF is
> returned if stream is NULL, or stream is not active, or there was an
> error when flushing buffered writes, or there was an error closing the
> underlying file descriptor."


IRIX man pages are not the Standard. There's no similar wording in
either the C or C++ standards.

Here's the wording from the C99 draft standard:

7.19.5.1 The fclose function

Synopsis

[#1]

#include <stdio.h>
int fclose(FILE *stream);

Description

[#2] The fclose function causes the stream pointed to by
stream to be flushed and the associated file to be closed.
Any unwritten buffered data for the stream are delivered to
the host environment to be written to the file; any unread
buffered data are discarded. The stream is disassociated
from the file. If the associated buffer was automatically
allocated, it is deallocated.

Returns

[#3] The fclose function returns zero if the stream was
successfully closed, or EOF if any errors were detected.



Brian


--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      02-04-2006
Default User <(E-Mail Removed)> wrote:

> 7.19.5.1 The fclose function


> Returns
>
> [#3] The fclose function returns zero if the stream was
> successfully closed, or EOF if any errors were detected.


Out of interest, could one not assume that "any error" will include
having passed 0 to fclose .. ?

regards
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-04-2006
Jakob Bieling wrote:

> Default User <(E-Mail Removed)> wrote:
>
> > 7.19.5.1 The fclose function

>
> > Returns
> >
> > [#3] The fclose function returns zero if the stream was
> > successfully closed, or EOF if any errors were detected.

>
> Out of interest, could one not assume that "any error" will include
> having passed 0 to fclose .. ?


Do you have some sort of support for that?

I think the fact that it differs from the implementation-specific man
page should tell you what you need to know.

Closing a null FILE* is UB.


Brian


--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      02-04-2006
On 3 Feb 2006 12:05:06 -0800, "kathy" <(E-Mail Removed)> wrote:

>if fopen failed, does it necessary to call fclose?
>
>I see an example like this:
>...
>stream = fopen(...);
>if(stream == NULL)
>{
>...
>}
>else
>{
>...
>}
>fclose(stream);
>...
>
>By my understanding, it should like this:
>...
>stream = fopen(...);
>if(stream == NULL)
>{
>...
>}
>else
>{
>...
>fclose(stream);
>}



else {
....
if (fclose(stream) == 0) {
// success
} else {
// error
}
}

Best wishes,
Roland Pibinger
 
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
fopen() with full path affecting subsequent fopen calls Michel Rouzic C Programming 4 04-28-2008 04:48 PM
seek and fgets and fputs fopen dave C++ 4 11-02-2006 10:10 PM
What is up with fopen??? FOpen and local directories Nonee HTML 2 10-25-2005 09:18 PM
Problem with fopen, linux, and RS-232, possibly Gregory Graham C Programming 3 12-09-2004 06:48 AM
zOS: fopen for record-IO with any LRECL and RECFM Rudi C Programming 2 01-23-2004 06:57 AM



Advertisments