问题
最近在学fstream时想要获得文件大小,结果发现用seekg和seekp后,tellg和tellp的返回值不一样。
测试代码
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file;
// test.txt : abcdefghijklmn
file.open(".\\test.txt", ios::ate | ios::in | ios::out |ios::binary);
streampos length = 0;
// file.seekp(ios::end);
length = file.tellp();
cout << "tellp:" << length << endl;
// file.seekp(ios::beg);
length = 0;
file.seekg(ios::end); // 注释掉这一行后两者结果相同
length = file.tellg();
cout << "tellg:" << length << endl;
// file.seekg(ios::beg);
return 0;
}
运行结果
未注释21行时:
PS D:\soft\code\c++\Pack> g++ .\test.cpp -o .\Output\test.exe
PS D:\soft\code\c++\Pack> .\Output\test.exe
tellp:14
tellg:2
注释21行时:
PS D:\soft\code\c++\Pack> g++ .\test.cpp -o .\Output\test.exe
PS D:\soft\code\c++\Pack> .\Output\test.exe
tellp:14
tellg:14
问题分析
在试验许多次之后, 发现只要把 file.seekg(ios::end); 给注释掉就会返回值相同
认为问题出在 ios::end 上,但又找不到问什么
PS:由于我其他项目只能使用 istream ,所以只能使用tellg,有谁能帮我解决这个问题吗?