如何使用 C++17 filesystem 库删除文件
要删除文件(例如 test.txt),使用 C++17 filesystem 库中的 remove:
remove-example.cpp
remove("test.txt");完整示例:
delete-cpp17.cpp
#include <experimental/filesystem>
using namespace std::experimental::filesystem;
int main() {
remove("test.txt");
}如果你使用 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