C++ examples for File Stream:File Operation
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 . j a v a 2s .com { 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; }