Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Brobanx

Is this safe?

This topic is 5787 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quick question. I THINK I read somewhere that if you file a file stream (fstream, ofstream, or ifstream), and you don''t call close() on it, the destructor will call close() automatically. I forget where I read this. Is it safe to have code like this, assuming the destructor will call close?
  
int main() {
  ofstream someFile("somefile.txt");
  someFile << "some text in some file.";
  return 0;
}
  

Share this post


Link to post
Share on other sites
Advertisement
Never make any assumptions. Yes, it is closed automatically in the deconstructor if opened via the constructor or open method, but you should be closing it manually anyway. If you were using C's fopen, you'd be responsible for closing it yourself anyway.

EDIT: If you have to ask if it's safe or not, just assume it isn't, and you won't run into too much trouble

[edited by - Zipster on November 18, 2002 2:13:53 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Zipster
If you have to ask if it''s safe or not, just assume it isn''t

Or even better: Check the manual

Share this post


Link to post
Share on other sites
quote:
Original post by Zipster
If you have to ask if it''s safe or not, just assume it isn''t
Why? If he asks it, then he''ll know the truth and he doesn''t have to assume anything.

Share this post


Link to post
Share on other sites
quote:
Original post by Zipster
...but you should be closing it manually anyway.
No, you leave it to the destructor. Explicitly closing the file is, at best, wasted typing.

Share this post


Link to post
Share on other sites
quote:
Original post by Beer Hunter
Explicitly closing the file is, at best, wasted typing.

In this case, perhaps, but not when you''re using the same fstream object in a loop. Getting into good habits (closing opened files, clearing failbits) will bide you well in the long run.

Share this post


Link to post
Share on other sites
Yeah, it is safe to assume it''ll close on its own. Though it isn''t exactly the best practice to do so.

Try to clean up things yourself when possible. You never know when you''ll be working on a bit of code that won''t clean up on its own and could potentially munch up ressources and memory by not deallocating them after the program ends. It''s a good idea to make it a habit to do this sort of stuff yourself.

Share this post


Link to post
Share on other sites
quote:
Original post by RuneLancer
Yeah, it is safe to assume it''ll close on its own. Though it isn''t exactly the best practice to do so.

Why not? Ever hear of RAII?
quote:

It''s a good idea to make it a habit to do this sort of stuff yourself.

It''s a better idea to understand the facilities you use and make informed decisions rather than write s/w based on superstitious practice.

Share this post


Link to post
Share on other sites
I''m not sure why everyone keeps saying "close it yourself." It''s perfectly fine to let the destructor do the closing! That''s why it''s there! Manually closing is for when you may want to open and close several times, etc. It''s perfectly safe to assume it will close because that''s what it''s defined to do.

Share this post


Link to post
Share on other sites
quote:
Original post by RuneLancer
Yeah, it is safe to assume it''ll close on its own. Though it isn''t exactly the best practice to do so.

Try to clean up things yourself when possible. You never know when you''ll be working on a bit of code that won''t clean up on its own and could potentially munch up ressources and memory by not deallocating them after the program ends. It''s a good idea to make it a habit to do this sort of stuff yourself.


No it isn''t, good programming habits dictates never to assume anything. What might be true on some architecture might not be on some other architecture. Unless you have a good garbage collector which C/C++ is pretty much lacking...




[Cyberdrek | the last true sorcerer | Spirit Mage - mutedfaith.com]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!