std::experimental::filesystem:: exists
From cppreference.net
<
cpp
|
experimental
|
fs
|
定義先ヘッダ
<experimental/filesystem>
|
||
|
bool
exists
(
file_status s
)
|
(1) | (filesystem TS) |
|
bool
exists
(
const
path
&
p
)
;
bool exists ( const path & p, error_code & ec ) |
(2) | (filesystem TS) |
指定されたファイルステータスまたはパスが既存のファイルまたはディレクトリに対応するかどうかをチェックします。
1)
次と同等:
status_known
(
s
)
&&
s.
type
(
)
!
=
file_type
::
not_found
。
2)
exists
(
status
(
p
)
)
または
exists
(
status
(
p, ec
)
)
と同等(シンボリックリンクは追従される)。例外を送出しないオーバーロードは、エラーが発生した場合
false
を返す。
目次 |
パラメータ
| s | - | チェックするファイルステータス |
| p | - | 検査するパス |
| ec | - | 非スローオーバーロードでのエラー報告用出力パラメータ |
戻り値
true 指定されたパスまたはファイルステータスが既存のファイルまたはディレクトリに対応する場合、 false それ以外の場合。
例外
1)
noexcept
指定子:
noexcept
2)
error_code
&
パラメータを取らないオーバーロードは、基盤となるOS APIエラーが発生した場合に
filesystem_error
をスローします。この例外は
p
を第1引数、OSエラーコードをエラーコード引数として構築されます。
std::
bad_alloc
はメモリ確保が失敗した場合にスローされる可能性があります。
error_code
&
パラメータを取るオーバーロードは、OS API呼び出しが失敗した場合にそれをOS APIエラーコードに設定し、エラーが発生しなかった場合は
ec.
clear
(
)
を実行します。このオーバーロードは
noexcept
指定を持ちます:
noexcept
注記
この関数によって提供される情報は、通常、ディレクトリ走査の副産物としても提供されます。ディレクトリ走査中、
exists(*iterator)
を呼び出すことは、
exists(iterator->status())
よりも効率が劣ります。
例
このコードを実行
#include <cstdint> #include <experimental/filesystem> #include <fstream> #include <iostream> namespace fs = std::experimental::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() { fs::create_directory("sandbox"); std::ofstream("sandbox/file"); // 通常ファイルを作成 fs::create_symlink("non-existing", "sandbox/symlink"); demo_exists("sandbox"); for (auto it = fs::directory_iterator("sandbox"); it != fs::directory_iterator(); ++it) demo_exists(*it, it->status()); // ディレクトリエントリからキャッシュされたステータスを使用 fs::remove_all("sandbox"); }
出力:
"sandbox" exists "sandbox/file" exists "sandbox/symlink" does not exist
関連項目
|
ファイル属性を決定する
シンボリックリンクのターゲットをチェックしてファイル属性を決定する (関数) |
|
|
ファイルタイプとパーミッションを表す
(クラス) |
|
|
このディレクトリエントリによって指定されたファイルのキャッシュされたステータス
このディレクトリエントリによって指定されたファイルのキャッシュされたsymlink_status (
std::experimental::filesystem::directory_entry
の公開メンバ関数)
|