Archived

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

ofstream question

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

I''d like to know if there is a way to test whether a file represented as a std::ofstream is open. It''s easy enough to see if it is NOT open by doing:
std::ofstream File;
File.open ("Test.txt", std::ios::out);
if (!File)
{
     // File not Open
}
 
It might sound a daft (and obvious) question, but how do I test that a file IS open WITHOUT have to use ugly code, e.g
if (!!File)
{
     // File is Open
}
 

Share this post


Link to post
Share on other sites
You should just look for C++ online references on google like this http://www.cplusplus.com/ref/indexr.html. Anyways there is a "is_open" method

Share this post


Link to post
Share on other sites
Make sure you know what you want to test for:

if(File.is_open()) will test whether the file is open.

if(!File) tests whether the last operation failed (here, open()). If the stream was associated with another (open) file, the file will still be open even though open() failed.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if (File)
{
}

It works because File is converted to void pointer which has the same effect as !!File when tested like this.

BTW, open the file like this:

std::ofstream File("Test.txt";

Notice that there''s no need for std::ios::out because it''s an ofstream and thus gets std::ios::out by default. And you can open the file right in the constructor without calling open().

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
if (File)
{
}

It works because File is converted to void pointer which has the same effect as !!File when tested like this.

BTW, open the file like this:

std::ofstream File("Test.txt";

Notice that there''s no need for std::ios::out because it''s an ofstream and thus gets std::ios::out by default. And you can open the file right in the constructor without calling open().


Your test will fail you if you use the wrong combination of flags to open the file. Use is_open, that''s why it''s there and it foolproof.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by fallenang3l
Your test will fail you if you use the wrong combination of flags to open the file. Use is_open, that''s why it''s there and it foolproof.
Can you give an example of where is_open is better, assuming I always open the file in the constructor (RAII style) and then immediately test for errors? I can''t say I''ve ever had problems with the idiom

?fstream x("...", ...);
if (!x) { /* error */}

What kind of combination of flags would cause !x==false but stream not open?

I think Fruny''s example is the only thing that shows the difference, but even there I''m pretty sure I''d be interested to know of the failure of attempting to open the new file so is_open would be insufficient. (and actually, I wouldn''t use open() at all)

Share this post


Link to post
Share on other sites