std::filesystem::space_info
來自 cppreference.com
< cpp | filesystem
定義於標頭檔案 <filesystem> |
||
struct space_info { std::uintmax_t capacity; |
(C++17 起) | |
表示由 filesystem::space
確定的檔案系統資訊。
目錄 |
[編輯] 成員物件
capacity |
檔案系統的總大小,以位元組為單位 (公有成員物件) |
free |
檔案系統上的可用空間,以位元組為單位 (公有成員物件) |
available |
非特權程序可用的空閒空間(可能等於或小於 free )(公有成員物件) |
[編輯] 非成員函式
operator== (C++20) |
比較兩個 space_info (函式) |
operator==(std::filesystem::space_info)
friend bool operator==( const space_info&, const space_info& ) = default; |
(C++20 起) | |
分別檢查兩個引數的 capacity
、free
和 available
是否相等。
此函式在常規 非限定 或 限定查詢 中不可見,並且只有當 std::filesystem::space_info 是引數的關聯類時,才能透過 實參依賴查詢 找到。
!=
運算子由 operator==
合成。
[編輯] 示例
執行此程式碼
#include <cstdint> #include <filesystem> #include <iostream> #include <locale> std::uintmax_t disk_usage_percent(const std::filesystem::space_info& si, bool is_privileged = false) noexcept { if (constexpr std::uintmax_t X(-1); si.capacity == 0 || si.free == 0 || si.available == 0 || si.capacity == X || si.free == X || si.available == X ) return 100; std::uintmax_t unused_space = si.free, capacity = si.capacity; if (!is_privileged) { const std::uintmax_t privileged_only_space = si.free - si.available; unused_space -= privileged_only_space; capacity -= privileged_only_space; } const std::uintmax_t used_space{capacity - unused_space}; return 100 * used_space / capacity; } void print_disk_space_info(auto const& dirs, int width = 14) { (std::cout << std::left).imbue(std::locale("en_US.UTF-8")); for (const auto s : {"Capacity", "Free", "Available", "Use%", "Dir"}) std::cout << "│ " << std::setw(width) << s << ' '; for (std::cout << '\n'; auto const& dir : dirs) { std::error_code ec; const std::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() { const auto dirs = {"/dev/null", "/tmp", "/home", "/proc", "/null"}; print_disk_space_info(dirs); }
可能的輸出
│ 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
[編輯] 另請參閱
(C++17) |
確定檔案系統上可用的空閒空間 (函式) |