Checks for errors opening file - C++ File Stream

C++ examples for File Stream:File Operation

Description

Checks for errors opening file

Demo Code

#include <fstream>          // for file functions
#include <iostream>
using namespace std;
int main()//from   w  w w  .  j a  va 2 s. c o  m
{
  ifstream file;
  file.open("a:test.dat");
  if (!file)
    cout << "\nCan't open GROUP.DAT";
  else
    cout << "\nFile opened successfully.";

  cout << "\nError state = " << file.rdstate();
  cout << "\ngood() = " << file.good();
  cout << "\neof() = " << file.eof();
  cout << "\nfail() = " << file.fail();
  cout << "\nbad() = " << file.bad() << endl;
  file.close();
  return 0;
}

Result


Related Tutorials