[用c ++ ios :: ate获取错误的文件大小

问题描述 投票:1回答:2

我正在尝试使用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的方法得到了错误的结果。我错过了什么,如何得到正确的结果?

c++ fstream
2个回答
0
投票

我知道了问题的原因。我的文件大约9 GB,不能用32位int变量表示。我使用了int64_t,该问题不再存在。


0
投票

下面是详细信息的链接,因为我认为file_size的返回类型需要类型转换:https://www.codingame.com/playgrounds/5659/c17-filesystem

© www.soinside.com 2019 - 2024. All rights reserved.