Remove file in C++ under UNIX Remove file in C++ under UNIX unix unix

Remove file in C++ under UNIX


Yep -- the C++ standard leaves this stuff up to the OS, so if you're on Linux (or any POSIX system), unlink() is what you've got.

The C standard provides remove(), which you could try, but keep in mind that its behavior is unspecified for anything other than a 'regular file', so it doesn't really shield you from getting into platform-specific filesystem details (links, etc).

If you want something higher-level, more robust, and more portable, check out Boost Filesystem.


The Standard includes a function called remove which does that. Though i would prefer boost.filesystem for that (if i already use boost anyway).

#include <cstdio>int main() {    std::remove("/home/js/file.txt");}


unlink() is defined by the POSIX standards, and hence will exist on any POSIX compatible system, and on quite a few that aren't POSIX compatible too.