Difference between casting ifstream to bool and using ifstream :: is_open ()

Perhaps a fictitious question, but I need a clear answer. Is there any difference in returning any of these functions

int FileExists(const std::string& filename)
{
  ifstream file(filename.c_str());
  return !!file;
}

int FileExists(const std::string& filename)
{
  ifstream file(filename.c_str());
  return file.is_open();
}

So in other words, my question is: does fstream output for bool produce exactly the same result as fstream :: is_open ()?

+5
source share
1 answer

No. is_openit only checks if there is a linked file, while casting to boolalso checks if the file is ready for I / O (for example, the stream is in good condition) (starting with C ++ 11).

is_open

Checks if a file has a stream associated file.

std::basic_ios::operator bool

true, -. , !fail().

+10

All Articles