Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::filesystem::file_size(3) C++ Standard Libarystd::filesystem::file_size(3)

NAME
       std::filesystem::file_size - std::filesystem::file_size

Synopsis
	  Defined in header <filesystem>
	  std::uintmax_t file_size( const std::filesystem::path& p );

	  std::uintmax_t  file_size(  const  std::filesystem::path&  p,	   (1)
       (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 fol-
       lowed).

	  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-de-
       fined.

	  The non-throwing overload returns static_cast<std::uintmax_t>(-1) on
       errors.

Parameters
	  p  - path to examine
	  ec - out-parameter for error reporting in the	non-throwing overload

Return value
	  The size of the file,	in bytes.

Exceptions
	  The overload that does not take a std::error_code& parameter throws
	  filesystem::filesystem_error	on  underlying	OS  API	 errors,  con-
       structed	with p as the
	  first	 path  argument	 and the OS error code as the error code argu-
       ment. The overload
	  taking a std::error_code& parameter sets it to the OS	API error code
       if an OS	API
	  call fails, and executes ec.clear() if no errors occur. Any overload
       not marked
	  noexcept may throw std::bad_alloc if memory allocation fails.

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<<(std::ostream& os, HumanReadable hr) {
		int i{};
		double mantissa	= hr.size;
		for (; mantissa	>= 1024.; mantissa /= 1024., ++i) { }
		mantissa = std::ceil(mantissa *	10.) / 10.;
		os << mantissa << "BKMGTPE"[i];
		return i == 0 ?	os : os	<< "B (" << hr.size << ')';
	    }
	};

	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";
		fs::file_size("/dev");
	    } catch(fs::filesystem_error& e) {
		std::cout << e.what() << '\n';
	    }

	    std::error_code ec;
	    for	(fs::path bin: {"cat", "mouse"}) {
		bin = "/bin"/bin;
		std::uintmax_t size = fs::file_size(bin, ec);
		if (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  en-
       try refers
		      (public	member	 function  of  std::filesystem::direc-
       tory_entry)

http://cppreference.com		  2022.07.31	 std::filesystem::file_size(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::filesystem::file_size&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help