Ifstream.read()读取的字节数多于指定的字节数

时间:2016-10-21 21:58:06

标签: c++ io iostream

我正在尝试从文件(二进制)中读取数据。我使用C ++ ifstream来访问数据。

文件的大小约为900KB,我可以告诉:

    stream.seekg(0, stream.end);
    cout << stream.tellg() << endl;
    stream.seekg(0, stream.beg);
    cout << stream.tellg() << endl;

当我尝试从文件中读取时,指针的移动实际上比我想读的更多。当我编写以下代码来跟踪它时(我知道缓冲区中的1个字节被覆盖):

    int counter = 0;
    while (stream.good()) {
        stream.read(buffer, 1);
        cout << stream.tellg() << " " << counter++  << " " << stream.gcount() << endl;
    }
    cout << counter << endl;

令我感到惊讶的是,这个事实并没有像反击那样增加。一些输出看起来像这样:

...
242770 25488 1
245760 25489 1
249345 25490 1
249346 25491 1
249347 25492 1
...

正如您所看到的,文件指针的位置有时会跳过1,有时会跳过2990或3585这样的数字。最后,流进入eofbit / failbit,尽管我读取的文件大小小于文件大小。这种行为有什么解释?注意:我使用的是windows / visual studio

0 个答案:

没有答案
相关问题