Datei mit C++17-filesystem-Bibliothek löschen

English Deutsch

To remove a file (for example test.txt) use remove from the C++17 filesystem library:

remove-example.cpp
remove("test.txt");

Vollständiges Beispiel:

delete-cpp17.cpp
#include <experimental/filesystem>
using namespace std::experimental::filesystem;

int main() {
    remove("test.txt");
}

Falls du GCC verwendest, musst du die Datei so kompilieren:

build-delete-cpp17.sh
g++ -o delete-cpp17 delete-cpp17.cpp -lstdc++fs

Du musst die stdc++fs-Bibliothek linken, damit die Funktionen aus der C++17-filesystem-Bibliothek deinem Programm zur Verfügung stehen.

Note that remove does not recursively remove directories! Use remove_all or see How to recursively delete directory using C++17 filesystem library


Check out similar posts by category: C/C++