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);
} 17 14788
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.
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
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
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.
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
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)
Default User <de***********@ yahoo.com> 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)
Jakob Bieling wrote: Default User <de***********@ yahoo.com> 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)
On 3 Feb 2006 12:05:06 -0800, "kathy" <yq*****@yahoo. com> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Martin Lucas-Smith |
last post by:
I wrote the function below as part of a larger class.
The fopen stage works, and, as according to the documentation at
www.php.net/fopen that succesfully creates a new file.
The fwrite stage returns false, however, on the first time the function is
run, but returns true the second time it is run.
Can anyone suggest why fwrite only works the second time?
|
by: JDJones |
last post by:
I'm trying to write a script that will read from a text list of songs
that I burned onto my CD (Albums011.txt), then write to the database
text the new text made ready for inserting into a database.
The entries from the Albums011.txt look like this:
Wayne Newton - Wild Cool & Swingin' - 03 - But Not For Me.mp3
Wayne Newton - Wild Cool & Swingin' - 04 - Wives And Lovers.mp3 etc.
and I want to manipulate it them to look in the...
|
by: Thomas Baruchel |
last post by:
Hi,
wondering about:
func1: setjmp() ; func2();
func2: {FILE *f; f = fopen(); func3(); fclose(f)}
func3 : if() longjmp; else return;
Note that FILE *fis a local variable in func2.
|
by: Aaron Couts |
last post by:
I have a program that writes to a log file. It's compiled on RH Linux 7.3
(Kernel 2.4.18-18.7). It's using fopen in append mode. When the file reaches
51200000 bytes in size, the program will no longer write to the file. When
this happens, fopen and fputs do not return an error. I've been researching
large file support for Linux, and it all has to do with the regular 2-gig file
size limit. If it's something obvious, sorry -- I'm a C...
|
by: git_cs |
last post by:
Hey, guys and gals
Somedays ago, I had asked for the DES algorithm in C language.
Although I have written the algorthim in C myself, I am facing a
peculiar problem, which I hope some of u guys and gals solve.
I use Turbo C++ version 3.0 and WINXP as the operating system.
Pls observe the following program.
1 #include<stdio.h>
2 #include<conio.h>
| |
by: Patrice |
last post by:
Hi,
I would to call fopen function several time in my application.
This application permits to read files which path is registered in a
configuration file.
For exemple:
File 1 = toto.txt
File 2 = tot2.txt
....File N = TotoN.txt
Then, I read each file (one by one), get adress of the beginning of
data and size of data.
|
by: lihua |
last post by:
Hi, Group!
I got one question here:
We all know that fclose() must be called after file operations to
avoid unexpected errors.But there are really cases when you forget to
do that!Just
like what happens in memory operations, everyone knows the importance
of
freeing the allocated memory, but there do have memory leaks from time
to
|
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 stream so that
the tape driver will write a filemark on the tape. Otherwise multiple
clumps of data saved to tape would all appear to be one big file on the
tape.
When the tape unit device was explicitly opened with fopen()
that's possible:...
|
by: subramanian100in |
last post by:
Consider the following program:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv)
{
if (argc != 2)
{
printf("Usage: <program-name<text-file>\n");
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |