• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function
    <filesystem>

    std::filesystem::directory_entry::file_size

    std::uintmax_t file_size() const;                             // (1)
    std::uintmax_t file_size(std::error_code& ec) const noexcept; // (2)
    

    概要

    ファイルサイズを取得する。

    戻り値

    値がキャッシュされている場合は、それを返す。キャッシュされていない場合は、

    例外

    #include <iostream>
    #include <filesystem>
    #include <fstream>
    
    namespace fs = std::filesystem;
    
    int main()
    {
      fs::create_directory("dir");
      fs::create_directory("dir/inner_dir");
      {
        std::ofstream file{"dir/a.txt", std::ios::binary};
        std::uint32_t value = 42;
        file.write(reinterpret_cast<char*>(&value), sizeof(value));
      }
    
      for (const fs::directory_entry& x : fs::directory_iterator("dir")) {
        if (x.is_regular_file()) {
          std::cout << x.path() << " : " << x.file_size() << std::endl;
        }
      }
    }
    

    出力

    "dir/a.txt" : 4
    

    バージョン

    言語

    • C++17

    処理系