1

据我了解,由于换行符,在文本模式下使用tellgandseekg可能会令人惊讶,但是如果没有换行符,我会得到一些奇怪的结果。我有以下代码:

#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 位。

谢谢你。

4

0 回答 0