C++ ifstream Open a file and check its status

Description

C++ ifstream Open a file and check its status

#include <iostream>
#include <fstream>
#include <cstdlib>  // needed for exit()
#include <string>
using namespace std;
int main()//from   w w  w  .  ja va  2s .co  m
{
   string filename = "prices.dat";  // place the filename up front
   ifstream inFile;
   inFile.open(filename.c_str());   // open the file
   if (inFile.fail())  // check for successful open
   {
      cout filename << " was not successfully opened\n Please check that the file currently exists." << endl;
      exit(1);
   }
   cout << "\nThe file has been successfully opened for reading.\n";
   return 0;
}



PreviousNext

Related