By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

fstream.close()

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ze****************@yahoo.com wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a
>
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.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.