How to delete file using C++17 filesystem library
To remove a file (for example test.txt
) use remove
from the C++17 filesystem library:
remove("test.txt");
Full example:
#include <experimental/filesystem>
using namespace std::experimental::filesystem;
int main() {
remove("test.txt");
}
In case you are using GCC, you need to compile the file like this:
g++ -o delete-cpp17 delete-cpp17.cpp -lstdc++fs
You need to link the stdc++fs
library so the functions from the C++17 filesystem library are available to your program.
Note that remove
does not recursively remove directories! Use remove_all
or seeĀ How to recursively delete directory using C++17 filesystem library