470,647 Members | 1,181 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

fstream.close()

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
2 1706
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
>
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.

Similar topics

7 posts views Thread by John Ratliff | last post: by
4 posts views Thread by adamrobillard | last post: by
9 posts views Thread by Someonekicked | last post: by
6 posts views Thread by wiso | last post: by
emaghero
1 post views Thread by emaghero | last post: by
5 posts views Thread by neowillis | last post: by
6 posts views Thread by Gaijinco | last post: by
6 posts views Thread by canilao | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.