【发布时间】:2012-04-13 12:43:55
【问题描述】:
我想知道seekg(0) 是否以及为什么不应该清除流的eofbit。
我现在已经阅读了所有流,因此已经到达EOF(但还没有设置failbit)并且想用seekg()回到有效位置并再次读取一些字符。在这种情况下,seekg(0) 似乎与eofbit 设置一起“工作”,但是一旦我尝试从流中读取,就会设置故障位。这个逻辑是正确的还是我的实施不好?我应该识别这种情况并手动清除eofbit(如果未设置failbit)?
编辑:
读者提供的以下程序在我的实现中给出了不同的结果( mingw32-c++.exe (TDM-2 mingw32) 4.4.1 ):
#include <sstream>
#include <iostream>
#include <string>
int main() {
std::istringstream foo("AAA");
std::string a;
foo >> a;
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0
foo.seekg(0);
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 0 0
foo >> a;
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0
foo >> a;
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 1
}
上面的 cmets 来自在他的实现中尝试过该程序的用户。我得到了这些结果:
1 0
1 0
1 1
1 1
【问题讨论】:
-
是的,您必须手动重置流的状态。
-
@Martin:似乎是旧标准与新标准的问题。