473,785 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

destructor and file descriptors

I fixed a bug today that went against my intuition. I am on Linux.
I had a class that fopen'ed some files. When I called delete
on these objects, I expected that the files would be closed
automatically, just as when an application terminates, but
evidently this is not the case. I guess I have to fclose the files
in the destructor.

Is this a correct assessment?

Jan 23 '07 #1
5 4070
"Digital Puer" <di**********@h otmail.comwrote :
I fixed a bug today that went against my intuition. I am on Linux.
I had a class that fopen'ed some files. When I called delete
on these objects, I expected that the files would be closed
automatically, just as when an application terminates, but
evidently this is not the case. I guess I have to fclose the files
in the destructor.

Is this a correct assessment?
Yes. If you use fstream instead, they will automatically close.
Jan 23 '07 #2
Digital Puer <di**********@h otmail.comwrote :
I fixed a bug today that went against my intuition. I am on Linux.
I had a class that fopen'ed some files. When I called delete
on these objects, I expected that the files would be closed
automatically, just as when an application terminates, but
evidently this is not the case. I guess I have to fclose the files
in the destructor.

Is this a correct assessment?
I would say yes. However, if you used std::fstream objects instead of
FILE*, then the fstream destructors would handle closing the files, so
you wouldn't need to manually close them in your destructor.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jan 23 '07 #3
On 22 Jan 2007 18:29:26 -0800, "Digital Puer" wrote:
>I fixed a bug today that went against my intuition. I am on Linux.
I had a class that fopen'ed some files. When I called delete
on these objects, I expected that the files would be closed
automaticall y, just as when an application terminates, but
evidently this is not the case. I guess I have to fclose the files
in the destructor.

Is this a correct assessment?
Merely calling fclose() in the destructor is not the right solution
when the file has been opened for writing. You need to check the
return value of fclose() and conduct appropriate action when fclose()
fails. Therefore the place for 'successful' fclose() is not the
destructor but another member function. In the destructor you can only
perform rollback, undo or cleanup activities which may include a
fclose() that terminates the unsuccessful file operation. The
situation is diffenent when the file has been opened for read only. In
that case you can put fclose() in the destructor only and ignore the
return value.

Best wishes,
Roland Pibinger
Jan 23 '07 #4
rp*****@yahoo.c om (Roland Pibinger) wrote:
Merely calling fclose() in the destructor is not the right solution
when the file has been opened for writing. You need to check the
return value of fclose() and conduct appropriate action when
fclose() fails.
Could you post some more details on this one? What kind of actions is
appropriate when fclose fails? I ask because basic_fstream's close
function returns void. Presumably, it does the appropriate action you
speak of?
Jan 23 '07 #5
On Tue, 23 Jan 2007 23:14:43 GMT, "Daniel T." wrote:
>rpbg123@...c om (Roland Pibinger) wrote:
>Merely calling fclose() in the destructor is not the right solution
when the file has been opened for writing. You need to check the
return value of fclose() and conduct appropriate action when
fclose() fails.

Could you post some more details on this one? What kind of actions is
appropriate when fclose fails?
This is more a design than an implementation question. One common
setting is that the whole file is read into memory, some changes are
made and the contents is written back to disk by overwriting the
original file. When fwrite() or fclose() fails the original file is
already destroyed and the new file is corrupt (half written).
One solution is that the original file is never overwritten, i.e.
never opened for write. This means that you write the contents to a
temporary file, close the temporary file (check the return value),
delete the original file and rename the temporary file to the original
file. Error-/failure-handling in that case just means that the
partially written temporary file is closed and deleted (the original
file remians unchanged). This kind of error-/failure-handling can also
be done in the destructor when the file has not been closed before
(the destructor performs only error-/failure-handling).
>I ask because basic_fstream's close
function returns void. Presumably, it does the appropriate action you
speak of?
AFAIK, fstream() has functions like good(), bad(), fail(), ... to let
the user ckeck the stream state.

Best wishes,
Roland Pibinger

Jan 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1812
by: François Pinard | last post by:
This question is a bit technical, but hopefully, this list will offer me good hints or nice solutions. Happily enough for me, it often does! :-) I would need to recognise and play with descriptor types, like: member descriptors method descriptors getset descriptors wrapper descriptors
5
2124
by: John Marshall | last post by:
Hi, Does anyone see a problem with doing: data = file("tata").read() Each time this is done, I see a new file descriptor allocated (Linux) but not released. 1) Will there ever be a point where I
5
3556
by: JG | last post by:
Hi all, Does anyone know how the implementations on Linux and Windows handle synchronization between a read and write FD open to the same file. For example, if I have 2 FD open to file X.txt. 1 I use for reading, the other for writing. If I write to position 125 on the write_FD, call flush, and then turn around and read from the read_FD position 125, am I guaranteed to get the result I just wrote?
36
2667
by: Roman Mashak | last post by:
Hello, All! I implemented simple program to eliminate entry from the file having the following structure (actually it's config file of 'named' DNS package for those who care and know): options { directory "/var/named"; listen-on { 192.168.11.22; 127.0.0.1; }; forwarders { 168.126.63.1; };
0
1384
by: jfigueiras | last post by:
>I have a problem with the module subprocess! As many other programs... I'm not sure what you mean by "non-standard file descriptors". The other program is free to open, read, write, etc any file he wants - are you trying to trap any file operation it may want to do? You *could* do such things -google for "code injection" and "API hooking"- but I doubt it's what you really want.
1
64196
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
5
5265
by: yinglcs | last post by:
I have a c/c++ program in linux. I would like to know if I kill my program (e.g. exit(1)), will it release all the file descriptors my program held (regardless if I call close(fd) of each file descriptor)? Thank you.
8
12022
ashitpro
by: ashitpro | last post by:
Understanding Ext-2 file system:chapter 1 The first block in each Ext2 partition is never managed by the Ext2 filesystem, because it is reserved for the partition boot sector. The rest of the Ext2 partition is split into block groups, each of which has the layout shown in Image 2. As you will notice from the figure, some data structures must fit in exactly one block, while others may require more than one block. All the block groups...
2
5442
by: DJ Dharme | last post by:
Hi all, I am writing a multi-threaded application in c++ running on solaris. I have a file which is updated by a single thread by appending data into the file and at same time the other threads are reading the content written into the file. Can anybody tell me is there a performance or any other gain (except for the multex locking) by using different file descriptors in each thread for the same file rather than using a single FD with a...
0
9646
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, 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...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
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...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
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
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.