最終更新日時(UTC):
が更新

履歴 編集

function
<filesystem>

std::filesystem::space(C++17)

namespace std::filesystem {
  space_info space(const path& p);                               // (1)
  space_info space(const path& p, std::error_code& ec) noexcept; // (2)
}

概要

指定されたパスの残り容量を取得する。

戻り値

  • POSIX環境であれば、statvfs()関数を使用して、残り容量を取得する。
  • 容量を決定できない場合、space_infoクラスのそのメンバ変数の値としては、static_cast<uintmax_t>(-1)を設定する
  • 容量をすべて取得できなかった場合は、

例外

Linux環境の例

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

void print_space(const char* name, std::uintmax_t bytes)
{
  std::uintmax_t mega_bytes = bytes / (1024 * 1024);
  std::cout << name << " : " << bytes << "[B]"
            << " (" << mega_bytes << "[MB])" << std::endl;
}

int main()
{
  fs::path p = "/";
  fs::space_info info = fs::space(p);

  std::cout << p << std::endl;
  print_space("capacity", info.capacity);
  print_space("free", info.free);
  print_space("available", info.available);
}

出力例

"/"
capacity : 1048580096[B] (1000[MB])
free : 1048580096[B] (1000[MB])
available : 1048580096[B] (1000[MB])

仮想環境で実行しているため、空き容量が減っていない。実環境で動かせるようになったら出力例を更新する。

バージョン

言語

  • C++17

処理系