C++ 实验性/文件系统 remove_all

标签 c++

我想在 C++17 中删除包含子文件夹和文件的文件夹。我正在使用 experimental/filesystem

namespace filesys = std::experimental::filesystem;
...

uintmax_t n = filesys::remove_all("tmp");
cout << "Deleted " << n << " files or directories\n";

但是当我运行这段代码时,程序抛出异常

terminate called after throwing an instance of 'std::experimental::filesystem::v1::__cxx11::filesystem_error'

what(): filesystem error: cannot remove all: Directory not empty [tmp]

Aborted

使用编译器 g++ 5.4.0

文档说:

Deletes the contents of p (if it is a directory) and the contents of all its subdirectories, recursively, then deletes p itself as if by repeatedly applying the POSIX remove. Symlinks are not followed (symlink is removed, not its target)

我的代码有问题吗?

最佳答案

抛出此错误的情况可能是 gcc 5.4 实现中的一个问题: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71313

使用较新版本的 gcc 不会抛出此类错误。例如,对于 gcc 7.4.0 上的空 remove_all 调用不会抛出此错误。

关于C++ 实验性/文件系统 remove_all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43698488/

相关文章:

c++ - 对象成员变量在函数中更新但不持久化

c++ - 在 Clang AST 中查找声明的父级

c++ - 从代码内部编译 QT 源代码

c++ - 我应该如何在 C++ 中读取和解析(真实的、正确的、全功能的)CSV?

c++ - Qt/C 如何读取当前在帧缓冲区中的图像?

c++ - 如何将 QTextBrowser 的多个实例打印到一个 PDF 文件中?

c++ - 当中间类跳过实现时,在继承层次结构中执行哪个虚方法?

c++ - 如何从 ifstream 获取格式化输入

c++ - 从代码中删除 SAL 注释是否安全?

c++ - 通过赋值初始化 C++ 对象时会发生什么?