Click to See Complete Forum and Search --> : Why should I close a file?
PeterRoddis
July 29th, 2008, 06:07 PM
Hi,
I building an app that write info to a file every few seconds using:
std::wofstream FileWrite
At the moment I am closing the file after writing each info message,
And I was wondering if I am the only one who is writing to the file why should i close it?
Can I leave a file open during the application life time?
Regards
Lindley
July 29th, 2008, 06:09 PM
No reason why not. Probably better to do so, actually.
PeterRoddis
July 29th, 2008, 06:11 PM
Thats what everyone say, but I still can't get a clear answer explaining why not.
GCDEF
July 29th, 2008, 06:32 PM
Thats what everyone say, but I still can't get a clear answer explaining why not.
That's because there is no reason why not.
Zaccheus
July 30th, 2008, 05:37 AM
Hi,
I building an app that write info to a file every few seconds using:
std::wofstream FileWrite
At the moment I am closing the file after writing each info message,
And I was wondering if I am the only one who is writing to the file why should i close it?
Can I leave a file open during the application life time?
Regards
I think there is at least one good reason to close the file: If the app crashes, you don't loose any data which may be hanging around in the output buffer.
A smaller concern is the general rule of not holding on to resources unnecessarily.
PeterRoddis
July 30th, 2008, 06:34 AM
I think there is at least one good reason to close the file: If the app crashes, you don't loose any data which may be hanging around in the output buffer.
A smaller concern is the general rule of not holding on to resources unnecessarily.
I thought that flushing the buffer would solve this issue.
GCDEF
July 30th, 2008, 07:34 AM
I thought that flushing the buffer would solve this issue.
It would.
potatoCode
July 31st, 2008, 04:40 AM
Hi PeterRoddis,
one reason I can think of is when you want to associate the same fstream object with another file.
fstream fout;
fout.open("abc.txt"); // ok
fout.open("def.txt") // Error. fstream object must be closed to open a new file
was this so obvious that it was uncessary for me to even bring it up??
sorry I'm still learning.
PeterRoddis
August 1st, 2008, 09:43 AM
Hi PeterRoddis,
one reason I can think of is when you want to associate the same fstream object with another file.
fstream fout;
fout.open("abc.txt"); // ok
fout.open("def.txt") // Error. fstream object must be closed to open a new file
was this so obvious that it was uncessary for me to even bring it up??
sorry I'm still learning.
It is a good point 8-)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.