std::filesystem::file_size(3)
π std::filesystem::file_size π
okay so bestie, this is std::filesystem::file_size from the C++ Standard Library β manual name std::filesystem::file_size(3) β and it is literally the βhow big is this file thoβ function. Itβs giving βI just want the byte countβ energy, and Iβm obsessed. π¦
π NAME π
std::filesystem::file_size - std::filesystem::file_size β¨
πΈ Synopsis πΈ
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, (2)
(since C++17)
std::error_code& ec ) noexcept;
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. So yeah, that part is a little βiykyk.β π
The non-throwing overload returns static_cast<std::uintmax_t>(-1) on errors. π
π§ Parameters π§
pβ path to examineecβ out-parameter for error reporting in the non-throwing overload
π₯ Return value π₯
The size of the file, in bytes. π
π Exceptions π
Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.
- Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed withpas the first path argument and the OS error code as the error code argument. - Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
π Example π
bestie, run this code if you want to see it in action π
// 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<<(std::ostream& os, HumanReadable hr)
{
int o{};
double mantissa = hr.size;
for (; mantissa >= 1024.; mantissa /= 1024., ++o);
os << std::ceil(mantissa * 10.) / 10. << "BKMGTPE"[o];
return o ? os << "B (" << hr.size << ')' : os;
}
};
int main(int, char const* argv[])
{
fs::path example = "example.bin";
fs::path p = fs::current_path() / example;
std::ofstream(p).put('a'); // create file of size 1
std::cout << example << " size = " << fs::file_size(p) << '\n';
fs::remove(p);
p = argv[0];
std::cout << p << " size = " << HumanReadable{fs::file_size(p)} <<
'\n';
try
{
std::cout << "Attempt to get size of a directory:\n";
[[maybe_unused]] auto x_x = fs::file_size("/dev");
}
catch (fs::filesystem_error& e)
{
std::cout << e.what() << '\n';
}
for (std::error_code ec; fs::path bin : {"cat", "mouse"})
{
bin = "/bin"/bin;
if (const std::uintmax_t size = fs::file_size(bin, ec); ec)
std::cout << bin << " : " << ec.message() << '\n';
else
std::cout << bin << " size = " << HumanReadable{size} <<
'\n';
}
}
π Possible output π
"example.bin" size = 1
"./a.out" size = 22KB (22512)
Attempt to get size of a directory:
filesystem error: cannot get file size: Is a directory [/dev]
"/bin/cat" size = 50.9KB (52080)
"/bin/mouse" : No such file or directory
π See also π
resize_fileβ changes the size of a regular file by truncation or zero-fill (C++17) (function)spaceβ determines available free space on the file system (C++17) (function)file_sizeβ returns the size of the file to which the directory entry refers (public member function ofstd::filesystem::directory_entry)
source/version: http://cppreference.com β date: 2024.06.10 β page: std::filesystem::file_size(3). mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms