简体   繁体   English

读取文件中的Eof位

[英]Eof bit in reading files

When i have something like this 当我有这样的事情

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

I was asking in that while for which field it is looking to continue? 我当时在问,它打算在哪个领域继续? For rdstate to evaluate if eof bit is set? 为了让rdstate评估eof位是否已设置? And for any field that is set except good bit that rdstate is set to non-zero value? 对于除了高位以外将rdstate设置为非零值的任何已设置字段?

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

In this the operator >>() returns an ifstream& . 在此, operator >>()返回ifstream& That ifstream has an operator bool() that will return true as long as no error flags are set. ifstream具有operator bool() ,只要未设置错误标志,该operator bool()将返回true。

So for every successful read f>>z evaluates to true and you continue the loop. 因此,对于每一次成功的读取, f>>z评估为true,然后继续循环。 As soon as you get a bad read( data type mismatch, end of file, stream error) then f>>z evaluates to false the the loop ends. 一旦读取错误(数据类型不匹配,文件结尾,流错误),则f>>z计算结果为false,则循环结束。

The stream states that ifstream has are: 该流指出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