A
adramolek
So... I'm trying to get used to using C++ ifstream (or ofstream)
instead of stdio (although I'm having second thoughts). Anyways, I
want to be able to display a meaningful error message if ifstream
fails to open a file, but nothing I read about ifstream says anything
about a reliable place to get an error message. Using stdio I can
simply do:
FILE *f = fopen(filename, "rb");
if (!f)
perror(filename);
So far the best I've been able to do using ifstream is:
ifstream f(filename, ios_base::in | ios_base::binary);
if (!f.is_open())
cerr << filename << ": tough luck!" << endl;
What is a reliable way to get a real error message? Even the example
at cplusplus.com:
http://www.cplusplus.com/reference/iostream/ifstream/is_open.html
Gives a crappy error message.
Thanks,
AJ
instead of stdio (although I'm having second thoughts). Anyways, I
want to be able to display a meaningful error message if ifstream
fails to open a file, but nothing I read about ifstream says anything
about a reliable place to get an error message. Using stdio I can
simply do:
FILE *f = fopen(filename, "rb");
if (!f)
perror(filename);
So far the best I've been able to do using ifstream is:
ifstream f(filename, ios_base::in | ios_base::binary);
if (!f.is_open())
cerr << filename << ": tough luck!" << endl;
What is a reliable way to get a real error message? Even the example
at cplusplus.com:
http://www.cplusplus.com/reference/iostream/ifstream/is_open.html
Gives a crappy error message.
Thanks,
AJ