最近在学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;
}
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
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,有谁能帮我解决这个问题吗?
seekg的参数用得不对。seekg有两种形式:
istream& seekg(streampos position); //相对于文件起始位置的偏移量
istream& seekg(streamoff offset, ios_base::seekdir dir); //相对于指定位置(ios::beg, ios::cur, ios::end)的偏移量
file.seekg(ios::end)对应的是istream& seekg(streampos position),表示相对于文件起始位置的偏移量,ios::end标志位对应的值是2,所以此时ios::end被理解为相对于文件其实位置的偏移量,所以tellg()的结果为2。注释掉这一句的话偏移量不变还是在文件末尾。或者把file.seekg(ios::end)改成file.seekg(0, ios::end)就可以定位到文件末尾。