如何使用 C++17 filesystem 库递归删除目录
要删除文件或目录(例如 my-directory),使用 C++17 filesystem 库中的 remove_all:
remove_all-example.cpp
remove_all("my-directory");这将递归删除 my-directory 及其所有子目录和文件。
完整示例:
delete-cpp17.cpp
#include <experimental/filesystem>
using namespace std::experimental::filesystem;
int main() {
remove_all("my-directory");
}如果你使用 GCC,你需要像这样编译文件:
build-delete-cpp17.sh
g++ -o delete-cpp17 delete-cpp17.cpp -lstdc++fs你需要链接 stdc++fs 库,以便 C++17 filesystem 库中的函数可用于你的程序。
如果你只想删除文件并且不想冒险删除整个目录树,使用 remove 而不是 remove_all,或参见我们之前的文章如何使用 C++17 filesystem 库删除文件
Check out similar posts by category:
C/C++
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow