I'm using fstream to open up .txt files with C++. So far it's working great. However, I'd like my console to display an error message if the input_file can't be opened. How should I go about this?
Snippet:
cin >> in_file_name ;
ifstream in_file(in_file_name.c_str());
in_file_str.assign(istreambuf_iterator<char>(in_file),
istreambuf_iterator<char>());
ifstream in_file(in_file_name.c_str());
if( in_file.fail() ) {
cerr << "Error!" << endl;
}
Use fail() to determine whether the ifstream was successfully opened or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With