我看到了我认为 istream::seekg 的奇怪行为。具体来说,当我寻找一个明显远离文件末尾的点时,它似乎没有设置故障位。
从我在文档中可以看出,应该设置故障位,但事实并非如此。
谁能解释这种行为?相关代码片段:
class Tester
{
...
void testTriggered()
{
fs.open("/pathtofile/testFile.TEST", std::ios_base::in|std::ios_base::binary);
prv_testbits("testTriggered(): OpeningFile");
fs.seekg(2000,std::ios_base::beg);
prv_testbits("testTriggered(): seekTwoThousand");
int g = fs.tellg();
std::cout << "get pointer is:" << g << std::endl;
}
void prv_testbits(std::string msg){
if (fs.fail()) {
std::cout << msg << ": failbit set." << std::endl;
} else {
std::cout << msg << ": failbit NOT set." << std::endl;
}
if (fs.bad()) {
std::cout << msg << ": badbit set." << std::endl;
}else {
std::cout << msg << ": badbit NOT set." << std::endl;
}
if (fs.eof()) {
std::cout << msg << ": eofbit set." << std::endl;
} else {
std::cout << msg << ": eofbit NOT set." << std::endl;
}
}
....
private:
std::ifstream fs;
};
输入文件由二十个字节组成:0123456789abcdefghij
样本运行的输出:
testTriggered(): OpeningFile: failbit NOT set.
testTriggered(): OpeningFile: badbit NOT set.
testTriggered(): OpeningFile: eofbit NOT set.
testTriggered(): seekTwoThousand: failbit NOT set.
testTriggered(): seekTwoThousand: badbit NOT set.
testTriggered(): seekTwoThousand: eofbit NOT set.
get pointer is:2000
g++ 版本信息: $ g++ -v 使用内置规范。目标:x86_64-linux-gnu 配置为:../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' -with-gxx-include-dir=/usr/include/c++/4.4 [ snip] gcc 版本 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
$ uname -a Linux hostx 2.6.32-36-server #79-Ubuntu SMP Tue Nov 8 22:44:38 UTC 2011 x86_64 GNU/Linux