ifstream::is_open
bool is_open ( );
ifstream
  cplusplus.com  

Check if a file has been opened.
  Checks if the stream is currently associated with a valid file buffer.

Parameters.

none
 

Return Value.
  If a file has successfully been opened (i.e. the stream is associated with a valid file buffer) the member function returns true,
  otherwise false is returned.

Example.

// ifstream::is_open
#include <iostream>
#include <fstream>
using namespace std;

int main () {

  ifstream infile;
  infile.open ("test.txt");
  if (infile.is_open())
  {
    while (infile.good())
      cout << (char) infile.get();
    infile.close();
  }
  else
  {
    cout << "Error opening file";
  }
  return 0;
}
This example uses is_open to check if the file has successfully been opened, if so prints out to stdout the content of the file, otherwise -like for example if the file to be opened doesn't exist- an error message is shown.

Basic template member declaration ( basic_ifstream<charT,traits> ):
bool is_open ();

See also.
  open
  ifstream class


© The C++ Resources Network, 2000