https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95749

            Bug ID: 95749
           Summary: std::filesystem::file_size returns wrong size for
                    large files on Windows
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: reiter.christoph at gmail dot com
  Target Milestone: ---

Downstream report: https://github.com/msys2/MINGW-packages/issues/6585

----

#include <filesystem>
#include <fstream>
#include <iostream>
#include <stdint.h>
#include <stdio.h>

int main(int argc, char** argv)
{

    uint64_t std_filesystem_file_size;
    uint64_t std_fstream_file_size;
    std::filesystem::path sourceFile = std::filesystem::current_path() /
"example_3GiB.bin";

    std::ofstream ofs(sourceFile);
    ofs << "this is some text in the new file\n";
    ofs.close();

    std::filesystem::resize_file(sourceFile,3221225472);
    std_filesystem_file_size = std::filesystem::file_size(sourceFile);

    std::ifstream in(sourceFile.string(), std::ifstream::ate |
std::ifstream::binary);
    std_fstream_file_size = in.tellg();

    std::cout << "3 GiB file:" << std::endl;
    std::cout << "std_filesystem_file_size: " << std_filesystem_file_size <<
std::endl;
    std::cout << "std_fstream_file_size: " << std_fstream_file_size <<
std::endl;
    return 0;
}

----

$ g++ -std=c++17 a.cpp
$ ./a.exe
3 GiB file:
std_filesystem_file_size: 18446744072635809792
std_fstream_file_size: 3221225472

----

The first value is wrong. My uneducated guess would be that
https://github.com/gcc-mirror/gcc/blob/41d6b10e96a1de98e90a7c0378437c3255814b16/libstdc%2B%2B-v3/src/filesystem/ops-common.h#L66
should use _wstat64() instead of _wstat and stat_type should be _stat64 instead
of _stat, otherwise things are limited to 32bit.

See
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions?view=vs-2019
for details.

Reply via email to