std::filesystem:: exists
|
定義先ヘッダ
<filesystem>
|
||
|
bool
exists
(
std::
filesystem
::
file_status
s
)
noexcept
;
|
(1) | (C++17以降) |
|
bool
exists
(
const
std::
filesystem
::
path
&
p
)
;
|
(2) | (C++17以降) |
|
bool
exists
(
const
std::
filesystem
::
path
&
p,
std::
error_code
&
ec
)
noexcept
;
|
(3) | (C++17以降) |
指定されたファイルステータスまたはパスが既存のファイルまたはディレクトリに対応するかどうかをチェックします。
目次 |
パラメータ
| s | - | チェックするファイルステータス |
| p | - | 検査するパス |
| ec | - | 非スローオーバーロードでのエラー報告用出力パラメータ |
戻り値
true 指定されたパスまたはファイルステータスが既存のファイルまたはディレクトリに対応する場合、 false それ以外の場合。
例外
noexcept
でマークされていないオーバーロードは、
メモリ確保に失敗した場合
std::bad_alloc
をスローする可能性があります。
オブジェクトが存在しない場合、ファイルシステム例外はスローされません(戻り値を使用してください)。
注記
この関数によって提供される情報は、通常、ディレクトリ走査の副産物としても提供されます。ディレクトリ走査中に exists ( * iterator ) を呼び出すことは、 exists ( iterator - > status ( ) ) よりも効率が劣ります。
例
#include <cstdint> #include <filesystem> #include <fstream> #include <iostream> namespace fs = std::filesystem; void demo_exists(const fs::path& p, fs::file_status s = fs::file_status{}) { std::cout << p; if (fs::status_known(s) ? fs::exists(s) : fs::exists(p)) std::cout << " exists\n"; else std::cout << " does not exist\n"; } int main() { const fs::path sandbox{"sandbox"}; fs::create_directory(sandbox); std::ofstream{sandbox/"file"}; // create regular file fs::create_symlink("non-existing", sandbox/"symlink"); demo_exists(sandbox); for (const auto& entry : fs::directory_iterator(sandbox)) demo_exists(entry, entry.status()); // use cached status from directory entry fs::remove_all(sandbox); }
出力:
"sandbox" exists "sandbox/symlink" does not exist "sandbox/file" exists
関連項目
|
(C++17)
(C++17)
|
ファイル属性を決定する
シンボリックリンクのターゲットをチェックしてファイル属性を決定する (関数) |
|
(C++17)
|
ファイルタイプとパーミッションを表す
(クラス) |
|
ディレクトリエントリが既存のファイルシステムオブジェクトを参照しているかチェックする
(
std::filesystem::directory_entry
の公開メンバ関数)
|