这里有人知道 C++ ifstream 的 get 指针在 read() 调用后可能会损坏吗?我看到了一些我无法解释的真正奇怪的行为。例如(说明性代码,而不是我实际运行的代码):
int main()
{
// datafile.bin is a 2MB binary file...
std::ifstream ifs( "datafile.bin", ios::binary );
ifs.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );
int data[100];
std::istream::pos_type current_pos = ifs.tellg();
// current_pos = 0, as you'd expect...
ifs.read( reinterpret_cast<char*>(data), 100 * sizeof(int) );
// throws no exception, so no error bits set...
std::streamsize bytes_read = ifs.gcount();
// gives 400, as you'd expect...
current_pos = ifs.tellg();
// current_pos = 0x1e1a or something similarly daft
return 0;
}
我的示例显示了一个数组读取,但即使在读取内置类型的单个值时也会发生这种情况;读取之前的 get 指针是正确的, gcount() 调用报告读取的正确字节数,但之后的 get 指针完全搞砸了。每次 read() 调用都不会发生这种情况——有时我会在一个东西出现之前通过一堆它们。什么可能会与 get 指针混淆?我在做一些非常愚蠢的事情吗?
任何和所有的帮助都非常感谢......
西蒙