首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::filesystem::remove

Defined in header <filesystem>

bool remove(const std::filesystem::path& p); bool remove(const std::filesystem::path& p, std::error_code& ec);

(1)

(since 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)

(since C++17)

1%29路径标识的文件或空目录。p被删除,就像被POSIX删除一样。移除符号链接没有跟随%28符号链接被移除,而不是它的目标%29

2%29删除p%28如果它是一个目录%29,并且它的所有子目录的内容都是递归的,那么删除p本身就好像通过反复应用POSIX移除符号链接没有跟随%28符号链接被移除,而不是它的目标%29

参数

p

-

path to delete

ec

-

out-parameter for error reporting in the non-throwing overload

返回值

1%29true如果文件被删除了,false如果它不存在的话。过载error_code&参数返回false关于错误。

2%29返回删除%28的文件和目录的数量,如果p不存在以%29开头的。过载error_code&参数返回static_cast<std::uintmax_t>(-1)关于错误。

例外

不占用std::error_code&参数抛文件系统[医]误差关于基础OS API错误,使用p作为第一个参数和操作系统错误代码作为错误代码参数。std::bad_alloc如果内存分配失败,则可能引发。过载std::error_code&参数,如果OSAPI调用失败,则将其设置为OSAPI错误代码,并执行ec.clear()如果没有错误发生。这个过载

noexcept规格:

noexcept

注记

在POSIX系统上,此函数通常调用unlinkrmdir视需要在Windows上RemoveDirectoryWDeleteFileW...

二次

代码语言:javascript
复制
#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";
}

二次

可能的产出:

二次

代码语言:javascript
复制
Deleted 2 files or directories

二次

另见

remove

erases a file (function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券