• Advertisement
Sign in to follow this  

[.net] C# equivalent to fstream::is_open()

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

C# gurus, In C++, we have this function to check if a file is currently open: fstream::is_open() What is the C# equivalent? p.s. Anyone notice gamedev.net has been very slow lately, takes my browser 30 sec to load a page each time!?

Share this post


Link to post
Share on other sites
Advertisement
Take a look at the System.IO namespace. You'll see that if a file fails to open the constructor throws an exception. Therefore, if no exception has been thrown then the file has been successfully opened. No need for an is_open function.

Skizz

Share this post


Link to post
Share on other sites
Thanks Skizz.

I am familiar with checking for file open failure via exception, but I still need the is_open function.

In my app, I want to write some data segments to a file at different times in a switch statement like this:


switch(data_segment_n)
{
case file_not_open:
open file and write data_segment_n to a specific location

case file_is_open:
write data_segment_n to a specific location
}
file_close();


This actual app is more complicated than this, and I don't want to close and re-open the file each time a segment is written. Anyway around it?

Share this post


Link to post
Share on other sites
The only way is to see if the object has been created as a valid object, which implies the file is open and ready for access. So, initialise the file object to null and use a test for null to determine if the file is open. null is a valid c# keyword.

Skizz

Share this post


Link to post
Share on other sites
If you're using a FileStream object, you can check the CanWrite property, this will be set to false when the file is closed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement