#### Archived

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

# ofstream question

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

## 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 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 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 on other sites
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 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 on other sites
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)

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633363
• Total Posts
3011514
• ### Who's Online (See full list)

There are no registered users currently online

×