Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::space

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

      Determines the information about the filesystem on which the pathnamep is located, as if by POSIXstatvfs.

      Populates and returns an object of typefilesystem::space_info, set from the members of the POSIXstruct statvfs as follows:

      The non-throwing overload sets all members tostatic_cast<std::uintmax_t>(-1) on error.

      Contents

      [edit]Parameters

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

      [edit]Return value

      The filesystem information (afilesystem::space_info object).

      [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]Notes

      space_info.available may be less thanspace_info.free.

      [edit]Example

      Run this code
      #include <cstdint>#include <filesystem>#include <iostream>#include <locale> std::uintmax_t disk_usage_percent(conststd::filesystem::space_info& si,bool is_privileged=false)noexcept{if(constexprstd::uintmax_t X(-1);        si.capacity==0|| si.free==0|| si.available==0||        si.capacity== X|| si.free== X|| si.available== X)return100; std::uintmax_t unused_space= si.free, capacity= si.capacity;if(!is_privileged){conststd::uintmax_t privileged_only_space= si.free- si.available;        unused_space-= privileged_only_space;        capacity-= privileged_only_space;}conststd::uintmax_t used_space{capacity- unused_space};return100* used_space/ capacity;} void print_disk_space_info(autoconst& dirs,int width=14){(std::cout<<std::left).imbue(std::locale("en_US.UTF-8")); for(constauto s:{"Capacity","Free","Available","Use%","Dir"})std::cout<<"│ "<<std::setw(width)<< s<<' '; for(std::cout<<'\n';autoconst& dir: dirs){std::error_code ec;conststd::filesystem::space_info si= std::filesystem::space(dir, ec);for(auto x:{si.capacity, si.free, si.available, disk_usage_percent(si)})std::cout<<"│ "<<std::setw(width)<<static_cast<std::intmax_t>(x)<<' ';std::cout<<"│ "<< dir<<'\n';}} int main(){constauto dirs={"/dev/null","/tmp","/home","/proc","/null"};    print_disk_space_info(dirs);}

      Possible output:

      │ Capacity       │ Free           │ Available      │ Use%           │ Dir            │ 84,417,331,200 │ 42,732,986,368 │ 40,156,028,928 │ 50             │ /dev/null│ 84,417,331,200 │ 42,732,986,368 │ 40,156,028,928 │ 50             │ /tmp│ -1             │ -1             │ -1             │ 100            │ /home│ 0              │ 0              │ 0              │ 100            │ /proc│ -1             │ -1             │ -1             │ 100            │ /null

      [edit]See also

      (C++17)
      information about free and available space on the filesystem
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/space&oldid=180241"

      [8]ページ先頭

      ©2009-2025 Movatter.jp