Sign in to follow this  

File open streams

This topic is 4310 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

Not that I am an expert or anything but I don't see why not. If you're going to be using the contents of the file stream often in your program then you may as well keep it open to eliminate multiple opening and closing calls.

-AJ

Share this post


Link to post
Share on other sites
What language are you using? If C++, be aware that file streams are implicitly closed by the destructors of those objects.

Personally, my advice is to never hold file stream objects as members of some other object (in C++, Java, Python, etc.), but otherwise assume the language can clean up for you (until proven otherwise - in C++ this is done by RAII; in Java and Python by garbage collection).

Share this post


Link to post
Share on other sites
Personally I avoid this like the plauge.

Its not particularly harmful to keep them open during the duration of your program because the OS takes care of closing open file handles anyway.

This is however bad practice.

And I'd actually use the system for File IO.

Share this post


Link to post
Share on other sites
In languages without deterministic destruction (such as Java) you should never trust the GC to free your resources (files, sockets, DB connections, ...) any time soon. Manual closing of files and judicious use of finally blocks is almost always required.

Share this post


Link to post
Share on other sites
C++. constructors/destructors are not my concern. I am aware of how destructors are called when an object goes out of scope. I was thinking about what would be more efficient, if I needed to access the file for read/write under real-time constraints.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by simon10k
C++. constructors/destructors are not my concern. I am aware of how destructors are called when an object goes out of scope. I was thinking about what would be more efficient, if I needed to access the file for read/write under real-time constraints.


Well I think that's a no-brainer.

You're well aware of the resource trade-off. If opening the file is an issue for you and you're willing to take the resource hit then do what you need to do. The techinque has a scale limitation, as I'm sure you know... the operating system is probably not going to let you do this with a million files.

Share this post


Link to post
Share on other sites

This topic is 4310 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this