std::experimental::filesystem:: remove, std::experimental::filesystem:: remove_all
From cppreference.net
<
cpp
|
experimental
|
fs
|
定義先ヘッダ
<experimental/filesystem>
|
||
|
bool
remove
(
const
path
&
p
)
;
bool remove ( const path & p, error_code & ec ) ; |
(1) | (filesystem TS) |
|
std::
uintmax_t
remove_all
(
const
path
&
p
)
;
std:: uintmax_t remove_all ( const path & p, error_code & ec ) ; |
(2) | (filesystem TS) |
1)
パス
p
によって特定されたファイルまたは空のディレクトリは、POSIXの
remove
によって削除されます。シンボリックリンクは追従されません(リンク先ではなく、シンボリックリンク自体が削除されます)。
2)
p
がディレクトリの場合、その内容とすべてのサブディレクトリの内容を再帰的に削除した後、
p
自体を、POSIXの
remove
を繰り返し適用するように削除します。シンボリックリンクは追従されません(リンク先ではなく、シンボリックリンク自体が削除されます)。
目次 |
パラメータ
| p | - | 削除するパス |
| ec | - | 例外を投げないオーバーロードでのエラー報告用出力パラメータ |
戻り値
1)
true
ファイルが削除された場合、
false
ファイルが存在しなかった場合。
error_code
&
引数を取るオーバーロードは、エラー時に
false
を返します。
2)
削除されたファイルとディレクトリの数を返します(
p
が最初から存在しなかった場合はゼロになる可能性があります)。
error_code
&
を引数に取るオーバーロードは、エラー時に
static_cast
<
std::
uintmax_t
>
(
-
1
)
を返します。
例外
The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument and the OS error code as the error code argument. std:: bad_alloc may be thrown if memory allocation fails. The overload taking an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
noexcept
仕様:
noexcept
注記
POSIXシステムでは、この関数は通常
unlink
と
rmdir
を必要に応じて呼び出し、Windowsでは
RemoveDirectoryW
と
DeleteFileW
を呼び出します。
例
このコードを実行
#include <cstdint> #include <experimental/filesystem> #include <iostream> namespace fs = std::experimental::filesystem; int main() { fs::path dir = fs::temp_directory_path(); fs::create_directories(dir / "abcdef/example"); std::uintmax_t n = fs::remove_all(dir / "abcdef"); std::cout << "Deleted " << n << " files or directories\n"; }
出力例:
Deleted 2 files or directories
関連項目
|
ファイルを削除する
(関数) |