繁体   English   中英

读取文件中的Eof位

[英]Eof bit in reading files

当我有这样的事情

ifstream f("file.txt");
int z=0;
while(f>>z)
cout<<z<<"\n";

我当时在问,它打算在哪个领域继续? 为了让rdstate评估eof位是否已设置? 对于除了高位以外将rdstate设置为非零值的任何已设置字段?

while(f>>z)
    cout<<z<<"\n";

在此, operator >>()返回ifstream& ifstream具有operator bool() ,只要未设置错误标志,该operator bool()将返回true。

因此,对于每一次成功的读取, f>>z评估为true,然后继续循环。 一旦读取错误(数据类型不匹配,文件结尾,流错误),则f>>z计算结果为false,则循环结束。

该流指出ifstream具有:

goodbit no error
badbit  irrecoverable stream error
failbit input/output operation failed (formatting or extraction error)
eofbit  associated input sequence has reached end-of-file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM