我正在尝试使用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的方法得到了错误的结果。我错过了什么?我如何才能得到正确的结果?
发布于 2019-12-23 13:25:37
我知道问题的原因了。我的文件大约有9 32长,不能用32位int变量来表示。我使用了int64_t,这个问题已经不存在了。
发布于 2019-12-23 22:23:45
下面是详细信息的链接,因为我认为file_size的返回类型需要进行类型转换:https://www.codingame.com/playgrounds/5659/c17-filesystem
https://stackoverflow.com/questions/59450399
复制相似问题