You can use the is_open() function on the ifstream. As so:
#include<iostream>
#include<fstream.h>
#include<string>
using namespace std;
int main()
{
ifstream inFile;
string inFileName;
cout << "Enter a filename: ";
cin >> inFileName;
inFile.open(inFileName.c_str());
if( ! inFile.is_open() ) {
cout << "File could not be opened!" << endl;
} else {
cout << "File was successfully opened!" << endl;
};
system ("pause");
return 1;
}
mike_2000_17
21st Century Viking
3,135 posts since Jul 2010
Reputation Points: 2,050
Solved Threads: 625
Skill Endorsements: 41