定义于头文件
<filesystem>
|
||
bool remove(const std::filesystem::path& p);
bool remove(const std::filesystem::path& p, std::error_code& ec); |
(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 | - | 不抛出重载中报告错误的输出参数 |
error_code&
参数的重载在错误时返回 false 。不接受 std::error_code& 参数的重载在底层 OS API 错误时抛出 filesystem_error ,以第一参数 p
和作为错误码参数的 OS 错误码构造。若内存分配失败则可能抛出 std::bad_alloc 。若 OS API 调用失败,则接受 std::error_code& 参数的重载设置该参数为 OS API 错误码,而若不出现错误则执行 ec.clear() 。
在 POSIX 系统上,此函数典型地按需调用 unlink
和 rmdir
,在 Windows 上则是 RemoveDirectoryW
和 DeleteFileW
。
#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
删除文件 (函数) |