std::filesystem::remove, std::filesystem::remove_all
来自cppreference.com
                    
                                        
                    < cpp | filesystem
                    
                                                            
                    |   定义于头文件  <filesystem>
  | 
||
|   bool remove(const std::filesystem::path& p); bool remove(const std::filesystem::path& p, std::error_code& ec) noexcept;  | 
(1) | (C++17 起) | 
|   std::uintmax_t remove_all(const std::filesystem::path& p); std::uintmax_t remove_all(const std::filesystem::path& p, std::error_code& ec);  | 
(2) | (C++17 起) | 
参数
| p | - | 要删除的路径 | 
| ec | - | 不抛出重载中报告错误的输出参数 | 
返回值
1) 若文件被删除则为 true ,若文件不存在则为 false 。接受 
error_code& 参数的重载在错误时返回 false 。异常
不接受 std::error_code& 参数的重载在底层 OS API 错误时抛出 filesystem_error ,以第一 path 参数 p 和作为错误码参数的 OS 错误码构造。若 OS API 调用失败,则接受 std::error_code& 参数的重载设置该参数为 OS API 错误码,而若不出现错误则执行 ec.clear() 。若内存分配失败,则任何不标记为 noexcept 的重载可能抛出 std::bad_alloc 。
注意
在 POSIX 系统上,此函数通常按需调用 unlink 和 rmdir ,在 Windows 上则是 RemoveDirectoryW 和 DeleteFileW 。
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| DR | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 3014 | C++17 |  remove_all 的 error_code 重载被标记为 noexcept 但能分配内存
 | 
移除 noexcept | 
示例
运行此代码
#include <iostream> #include <cstdint> #include <filesystem> namespace fs = std::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
参阅
|    删除文件   (函数)  |