tellg()在文本模式下失败(不涉及新行)

时间:2013-05-17 12:14:16

标签: c++ ifstream

据我所知,在文本模式中使用tellgseekg因为换行符而令人惊讶,但是如果没有换行符,我会得到一些奇怪的结果。我有以下代码:

#include <fstream>

int main()
{
    using namespace std;

    ifstream file("test.txt");

    char a;
    file >> a;

    ifstream::streampos curPos = file.tellg(); // _Fpos = 1
    ifstream::streampos endPos = file.seekg(0, ios::end).tellg(); // _Fpos = 5
    ifstream::streampos testPos = file.seekg(curPos).tellg(); // _Fpos = 1

    file.putback(a);

    testPos = file.tellg(); // _Fpos = -511 in text mode, 
                            // _Fpos = 0 in binary mode
}

text.txt内容:

0 1 2

如果在文本模式下打开文件,则最后tellg会返回错误的结果。如果我尝试seekg到这个位置,它会破坏文件流。我做错了什么或是某种错误?

我正在使用VS2010,Win 7 64位。

谢谢。

0 个答案:

没有答案