我想在 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/