【发布时间】:2012-02-04 12:43:00
【问题描述】:
我刚刚在 C++ 中遇到了一个奇怪的情况。我正在做类似的事情:
istream in;
// ...
in.get(); // get a char (which turns out to be the last)
// curiously ios::eof bit doesn't get set just yet
c = in.peek(); // attempt to peek, return EOF and now set ios::eof bit
if(c == EOF) {
// realize shouldn't have gotten the last char for some reason
in.unget(): // attempt to unget, *fails* (because ios:eof bit was set)
}
现在我很好奇为什么 peek 设置 eof 位;我觉得这非常不直观。它应该只是偷看不实际消耗任何东西,也不应该改变流状态。另外,为什么unget 随后不起作用?当good() 为假时,标准是否要求所有操作都是nop?
【问题讨论】: