Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::file_size

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<filesystem>
      (1)(since C++17)
      std::uintmax_t file_size(conststd::filesystem::path& p,
                               std::error_code& ec)noexcept;
      (2)(since C++17)

      Ifp does not exist, reports an error.

      For a regular filep, returns the size determined as if by reading thest_size member of the structure obtained by POSIXstat (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 returnsstatic_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 markednoexcept may throwstd::bad_alloc if memory allocation fails.

      1) Throwsstd::filesystem::filesystem_error on underlying OS API errors, constructed withp as the first path argument and the OS error code as the error code argument.
      2) Sets astd::error_code& parameter to the OS API error code if an OS API call fails, and executesec.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:friendstd::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,charconst* argv[]){    fs::path example="example.bin";    fs::path p= fs::current_path()/ example;std::ofstream(p).put('a');// create file of size 1std::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(conststd::uintmax_t size= fs::file_size(bin, ec); ec)std::cout<< bin<<" : "<< ec.message()<<'\n';elsestd::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

      [edit]See also

      changes the size of a regular file by truncation or zero-fill
      (function)[edit]
      (C++17)
      determines available free space on the file system
      (function)[edit]
      returns the size of the file to which the directory entry refers
      (public member function ofstd::filesystem::directory_entry)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/file_size&oldid=159791"

      [8]ページ先頭

      ©2009-2025 Movatter.jp