我正在尝试使用C++获取文件的大小。代码如下:
ifstream fin(filepth, ios::in | ios::binary | ios::ate);
if (!fin.is_open()) {
cout << "cannot open file:" << filepth << endl;
}
int len = fin.tellg();
fin.seekg(0, ios::beg);
fin.clear();
cout << "file length is: " << len << endl;
cout << "file length is: " << fs::file_size(fs::path(filepth)) << endl;
原来使用
ios::ate
的方法得到了错误的结果,我错过了什么并且如何得到正确的结果?
pos_type
,而不是int
。pos_type
的问题在于它必须能够报告错误时返回-1
,因此可以使用long
(或32位上的long long
)或ssize_t
。 - David C. Rankin