std::filesystem::file_size
From cppreference.com
< cpp | filesystem
Defined in header <filesystem>
|
||
std::uintmax_t file_size( const std::filesystem::path& p ); |
(1) | (since C++17) |
std::uintmax_t file_size( const std::filesystem::path& p, std::error_code& ec ) noexcept; |
(2) | (since C++17) |
If p does not exist, reports an error.
For a regular file p, returns the size determined as if by reading the st_size
member of the structure obtained by POSIX stat
(symlinks are followed).
The result of attempting to determine the size of a directory (as well as any other file that is not a regular file or a symlink) is implementation-defined.
The non-throwing overload returns static_cast<std::uintmax_t>(-1) on errors.
Contents |
[edit] Parameters
p | - | path to examine |
ec | - | out-parameter for error reporting in the non-throwing overload |
[edit] Return value
The size of the file, in bytes.
[edit] Exceptions
Any overload not marked noexcept
may throw std::bad_alloc if memory allocation fails.
1) Throws std::filesystem::filesystem_error on underlying OS API errors, constructed with p as the first path argument and the OS error code as the error code argument.
2) Sets a std::error_code& parameter to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur.
[edit] Example
Run this code
#include <cmath> #include <filesystem> #include <fstream> #include <iostream> namespace fs = std::filesystem; struct HumanReadable { std::uintmax_t size{}; private: friend std::ostream& operator<<(