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) | (文件系统 TS) |
|
std::
uintmax_t
remove_all
(
const
path
&
p
)
;
std:: uintmax_t remove_all ( const path & p, error_code & ec ) ; |
(2) | (文件系统 TS) |
目录 |
参数
| 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
参见
|
删除文件
(函数) |