C++ ifstream File Status Functions

Introduction

PrototypeDescription
fail() Returns a Boolean true if the file hasn't been opened successfully; otherwise, returns a Boolean false value.
eof()Returns a Boolean true if a read has been attempted past the end-of-file; otherwise, returns a Boolean false value.
good() Returns a Boolean true value while the file is available for program use.
bad()Returns a Boolean true value if an error occurs that results in data loss when reading from or writing to a stream; otherwise, returns a false.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()/*w w  w  .  j  av  a 2 s  .  com*/
{
   ifstream inFile;
   inFile.open("prices.dat");
   if (inFile.fail())
   {
      cout << "\nThe file was not successfully opened" << endl;
      exit(1);
   }
   cout  << "\nThe file has been successfully opened for reading." << endl;
   return 0;
}



PreviousNext

Related