Namespaces
Variants

std::experimental::filesystem:: remove, std::experimental::filesystem:: remove_all

From cppreference.net
定义于头文件 <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)
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

参见

删除文件
(函数)