fstream.close()

G

Guest

Hi,

I've been including code like this in my programs for some time:

fstream fin ("c:\\dave.txt", ios::in);
if (!fin)
{
return -1; //or throw an exception, or return false, or
//whatever
}
//do things
fin.close();

However, I've recently started to wonder if I should include
fin.close() in the stuff that executes if(!fin), and also, if I'm not
supposed to, whether anything bad will happen if I do?

I'm also trying to put a few functions together for comp.sys.sinclair,
and would like these to be as compatible as possible, so if anyone
knows the answer to my questions for <fstream.h> as well as <fstream>,
it would be appreciated.

Thanks!

James M.
 
K

Karl Heinz Buchegger

Hi,

I've been including code like this in my programs for some time:

fstream fin ("c:\\dave.txt", ios::in);
if (!fin)
{
return -1; //or throw an exception, or return false, or
//whatever
}
//do things
fin.close();

However, I've recently started to wonder if I should include
fin.close() in the stuff that executes if(!fin), and also, if I'm not
supposed to, whether anything bad will happen if I do?

fin as an fstream object. Objects of this type will close the
file when their destructor runs. So in the above you could
leave out the close and still be save. When the fin object
finally gets out of scope when the function is left, its
destructor will close the file.
I'm also trying to put a few functions together for comp.sys.sinclair,
and would like these to be as compatible as possible, so if anyone
knows the answer to my questions for <fstream.h> as well as <fstream>,
it would be appreciated.

fstream.h is not a standard C++ header, so all bets are of of what the
classes in it do. However it is a save bet that those stream classes
behave in very much the same way with respect to this.
 
J

John Harrison

Hi,

I've been including code like this in my programs for some time:

fstream fin ("c:\\dave.txt", ios::in);
if (!fin)
{
return -1; //or throw an exception, or return false, or
//whatever
}
//do things
fin.close();

However, I've recently started to wonder if I should include
fin.close() in the stuff that executes if(!fin), and also, if I'm not
supposed to, whether anything bad will happen if I do?

It is not necessary to close file streams, they close automatically when the
file stream destructor executes.

john
 
F

Fraser Ross

I've been including code like this in my programs for some time:

fstream fin ("c:\\dave.txt", ios::in);
if (!fin)
{
return -1; //or throw an exception, or return false, or
//whatever
}
//do things
fin.close();

However, I've recently started to wonder if I should include
fin.close() in the stuff that executes if(!fin), and also, if I'm not
supposed to, whether anything bad will happen if I do?

Its a good idea to include a closing action before opening a new file. I
always do even if it often does nothing. Using ! on the stream gets the
state of the good flag but you want to know if the stream has opened the
file which isn't the same. The is_open member function is there for that.

Fraser.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top