名前空間
変種
操作

std::filesystem::directory_entry::is_directory

From cppreference.com
 
 
 
 
bool is_directory() const;
(1) (C++17以降)
bool is_directory( std::error_code& ec ) const noexcept;
(2) (C++17以降)

参照しているオブジェクトがディレクトリであるかどうかをチェックします。実質的には以下を返します。

目次

[編集] パラメータ

エラーコード - 例外を投げないオーバーロードでのエラー報告のための出力パラメータ

[編集] 戻り値

true 参照されているファイルシステムオブジェクトがディレクトリである場合、それ以外の場合は false

[編集] 例外

noexcept とマークされていないオーバーロードは、メモリ割り当てが失敗した場合に std::bad_alloc をスローする可能性があります。

1) 基盤となるOS APIエラーが発生した場合、最初のパス引数としてp、エラーコード引数としてOSのエラーコードとともに構築されたstd::filesystem::filesystem_error を投げます。
2) OS API呼び出しが失敗した場合、std::error_code& パラメータにOS APIのエラーコードが設定されます。エラーが発生しなかった場合は、ec.clear() が実行されます。

[編集]

#include <filesystem>
#include <iostream>
#include <string_view>
 
namespace fs = std::filesystem;
 
void check_directory(fs::directory_entry const& d, std::string_view rem = "")
{
    std::cout << "is_directory(" << d << "): " << d.is_directory() << rem << '\n';
}
 
int main()
{
    fs::directory_entry d1(".");
    fs::directory_entry d2("file.txt");
    fs::directory_entry d3("new_dir");
 
    std::cout << std::boolalpha;
 
    check_directory(d1);
    check_directory(d2);
    check_directory(d3, " (has not been created yet).");
 
    std::filesystem::create_directory("new_dir");
 
    check_directory(d3, " (before refresh).");
    d3.refresh();
    check_directory(d3, " (after refresh).");
}

実行結果の例

is_directory("."): true
is_directory("file.txt"): false
is_directory("new_dir"): false (has not been created yet).
is_directory("new_dir"): false (before refresh).
is_directory("new_dir"): true (after refresh).

[編集] 関連項目

指定されたパスがディレクトリを参照しているか確認する
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)