std::filesystem::directory_entry::file_size

If the file size is cached in this directory_entry, returns the cached value. Otherwise, returns:

# Declarations

std::uintmax_t file_size() const;

(since C++17)

std::uintmax_t file_size( std::error_code& ec ) const noexcept;

(since C++17)

# Parameters

# Return value

The size of the referred-to filesystem object.

# Example

#include <cmath>
#include <cstdint>
#include <filesystem>
#include <iostream>
 
struct HumanReadable
{
    std::uintmax_t size{};
 
    template<typename Os> friend Os& operator<<(Os& os, HumanReadable hr)
    {
        int i{};
        double mantissa = hr.size;
        for (; mantissa >= 1024.0; mantissa /= 1024.0, ++i)
        {}
        os << std::ceil(mantissa * 10.0) / 10.0 << i["BKMGTPE"];
        return i ? os << "B (" << hr.size << ')' : os;
    }
};
 
int main(int argc, const char* argv[])
{
    const auto dir = argc == 2 ? std::filesystem::path{argv[1]}
                               : std::filesystem::current_path();
 
    for (std::filesystem::directory_entry const& entry : 
         std::filesystem::directory_iterator(dir))
        if (entry.is_regular_file())
            std::cout << entry.path().filename() << " size: "
                      << HumanReadable{entry.file_size()} << '\n';
}

# See also