C ++ istream EOF不保证故障位吗? [英] C++ istream EOF does not guarantee failbit?
问题描述
我在堆栈溢出中阅读了问题,其中istream的eofbit设置了 file
,但未设置故障位。在这种情况下,file为true,而file.eof()为true,但file.good()为false。例如,文件大小恰好为一个字节:
I read a question on Stack Overflow where the eofbit for the istream file
is set but the failbit is not. In that case file is true and file.eof() is true but file.good() is false. For example with a file exactly one byte in size:
ifstream file("one.txt");
assert( file.is_open() );
for( int i = 0; i < 2; ++i )
{
char chars[255] = {0};
file.getline(chars, 2);
//file.read( chars, 2 );
cout << "file: " << !!file << endl;
cout << "good: " << file.good() << endl;
cout << "eof: " << file.eof() << endl;
cout << "fail: " << file.fail() << endl;
cout << "bad: " << file.bad() << endl;
cout << endl;
}
这是输出:
file: 1
good: 0
eof: 1
fail: 0
bad: 0
file: 0
good: 0
eof: 1
fail: 1
bad: 0
如果我注释掉getline()并改用read(),我会得到:
If I comment out the getline() and use read() instead, I get this:
file: 0
good: 0
eof: 1
fail: 1
bad: 0
file: 0
good: 0
eof: 1
fail: 1
bad: 0
在两种情况下,我都在循环的第一次迭代中读取文件末尾的内容。为什么一个EOF失败而一个不失败?另一个线程中的答案是只要遇到文件末尾而没有试图在文件后读取。看后面吗?
In both cases I'm reading past the end of the file in the loop's first iteration. Why is one EOF and fail and one isn't? The answer in the other thread says "whenever you encounter the end of a file without attempting to read behind it." read behind it? What does that mean?
推荐答案
eofbit
由 getline
方法是否停止读取,因为它碰到了流(文件)的末尾,而不是查找分隔符。在这种情况下, getline
不会设置失败位
,这是合乎逻辑的,因为它没有t失败:它读取了一些数据。
The eofbit
is set by the getline
method if it stops reading because it hits the end of the stream (file), rather than by finding the delimiter character. getline
does not set the failbit
in that case, which is logical because it didn't fail: it read some data.
一旦设置了 eofbit
,对该流的下一个读取操作将设置失败位
,因为内部岗哨功能(几乎在每次输入操作开始时都会调用)将设置失败位
如果设置了 eofbit
。
Once the eofbit
is set, the next read operation on that stream will set the failbit
, because the internal sentry function, called at the start of just about every input operation, will set failbit
if eofbit
is set.
通常, eofbit
表示输入操作已到达输入序列的末尾,而失败位
表示输入操作未能读取期望的字符(两个引号均来自C ++标准的 [ios.types]
部分;§ 27.5.3.1,表124,在我最近提出的草案中。)作为 getline
示例显示,输入操作很有可能在读取输入序列的结尾的同时仍读取某些内容。
In general, the eofbit
"indicates that an input operation reached the end of an input sequence", while failbit
"indicates that an input operation failed to read the expected characters" (both quotes are from the C++ standard, section [ios.types]
; §27.5.3.1, table 124, in the latest draft I have lying around.) As the getline
example shows, it is quite possible for an input operation to read the end of an input sequence, while still reading something.
另一个,少一些mal(因此可能是错误的)的查看方式是,如果读取操作到达文件末尾,则设置 eofbit
,而 failbit
。调用 getline
和 read
的调用中的 2
表示不同东西在 getline
中,它是输入操作的最大个字符(最小为1);对于 read
,它是 precise 个字符。
Another, somewhat less formal -- and therefore possibly incorrect -- way of looking at this is that eofbit
is set if the read operation reached the end of the file, whereas the failbit
is set if the read operation could not read the minimum number of characters it required. The 2
in the calls to getline
and read
mean different things; in getline
, it is the maximum number of characters for the input operation (the minimum number is 1); in the case of read
, it is the precise number of characters.
这篇关于C ++ istream EOF不保证故障位吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!