ifstream错误 [英] ifstream errors
问题描述
所以......我正在尝试习惯使用C ++ ifstream(或ofstream)
而不是stdio(虽然我有第二个想法)。无论如何,我希望能够显示一条有意义的错误消息,如果ifstream
无法打开文件,但我没有看到任何关于ifstream说什么的内容
关于获取错误消息的可靠位置。使用标准输入输出我可以
简单地做:
FILE * F =的fopen(文件名," RB");
如果(!f)
perror(文件名);
到目前为止,我能用ifstream做的最好的是:
ifstream f(filename,ios_base :: in | ios_base :: binary);
if(!f.is_open())
cerr< ;< filename<< :运气不好! << endl;
获取真实错误消息的可靠方法是什么?即使是cplusplus.com上的例子
:
http://www.cplusplus.com/reference/i...m/is_open.html
给蹩脚错误信息。
谢谢,
AJ
3月27日,4:29 am,adramo ... @ gmail.com写道:
获取真实错误信息的可靠方法是什么?
哦,还有,与此相关,如何判断>是否失败?假设我正在阅读
来自在文本模式下打开的ifstream的4个整数。使用标准输入输出:
INT A,B,C,d;
如果(的fscanf(infile中,"%I%I%I%I" ;, & a,& b,& c,& d)!= 4)
fprintf(stderr,错误解析行\ n);
>
但是使用ifstream:
int a,b,c,d;
infile<< a<< b<< c<< d;
//如何检查失败...?
谢谢
3月27日,4日:上午33点,adramo ... @ gmail.com写道:
infile<< a<< b<< c<< d;
即,>>
在27 MRZ,9点33分,adramo .. 。@ gmail.com写道:
[..]
但是使用ifstream:
int a,b,c,d;
infile<< a<< b<< c<< d;
//如何检查失败......?
if(infile.fail()){
....
}
最好,
Michael
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/i...m/is_open.html
Gives a crappy error message.
Thanks,
AJ
On Mar 27, 4:29 am, adramo...@gmail.com wrote:What is a reliable way to get a real error message?Oh, also, related to this, how do I tell if >fails? Say I am reading
4 integers from an ifstream opened in text mode. Using stdio:
int a, b, c, d;
if (fscanf(infile, "%i%i%i%i", &a, &b, &c, &d) != 4)
fprintf(stderr, "error parsing line\n");
But using ifstream:
int a, b, c, d;
infile << a << b << c << d;
// how to check for failure...?
Thanks
On Mar 27, 4:33 am, adramo...@gmail.com wrote:infile << a << b << c << d;
That is, >>
On 27 Mrz., 09:33, adramo...@gmail.com wrote:
[..]But using ifstream:
int a, b, c, d;
infile << a << b << c << d;
// how to check for failure...?if ( infile.fail() ) {
....
}
best,
Michael
这篇关于ifstream错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!