std::experimental::filesystem::file_size

Header: <experimental/filesystem>

Returns the size of the regular file p, determined as if by reading the st_size member of the structure obtained by POSIX stat (symlinks are followed).

# Declarations

std::uintmax_t file_size( const path& p );
std::uintmax_t file_size( const path& p, error_code& ec );

(filesystem TS)

# Parameters

# Return value

The size of the file, in bytes.

# Example

#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
 
int main()
{
    fs::path p = fs::current_path() / "example.bin";
    std::ofstream(p).put('a'); // create file of size 1
    std::cout << "File size = " << fs::file_size(p) << '\n';
    fs::remove(p);
 
    try
    {
        fs::file_size("/dev"); // attempt to get size of a directory
    }
    catch (fs::filesystem_error& e)
    {
        std::cout << e.what() << '\n';
    }        
}

# See also