Error Conditions with Streams

  ifstream myIn;
  myIn.open("datafile.in", ios::in);

  // test to see if there was an error; if there was 
  // an error, it is not safe to proceed
  if ( myIn.fail() )
    cout << "Unable to open input file" << endl;
  else
  {
     while ( !myIn.eof() )
     {
        // process data in file
     }
     myIn.close();   // close the file here
  } 
Previous page
Next page

Back to Lesson 13 Index
Back to Outline