What is the difference between remove and unlink functions in C++?
5 Answers
Apart from the fact that unlink is unix-specific (as pointed out by Chris), we read in the POSIX manual:
If path does not name a directory, remove(path) is equivalent to unlink(path). If path names a directory, remove(path) is equivalent to rmdir(path).
As for the directory-passed unlink
, we read:
The path argument must not name a directory unless the process has appropriate privileges and the implementation supports using unlink() on directories. (...) Applications should use rmdir() to remove a directory.
The remove()
function removes the file or directory specified by path.
If path specifies a directory, remove(path)
is the equivalent of
rmdir(path)
. Otherwise, it is the equivalent of unlink(path)
.
From: man remove
.
Good Luck ;)
-
Note that that's specific to Unix-like systems (which is perfectly appropriate given the tags on the question). The ISO C standard defines the
remove
function; it says nothing about directories. POSIX extends its behavior as you describe. Feb 28, 2013 at 1:33
unlink is not unix-specific, i don't know why people're saying that. see io.h. although you'll probably have to do something like
#define unlink _unlink
http://msdn.microsoft.com/en-us/library/1c3tczd6%28v=VS.100%29.aspx
http://msdn.microsoft.com/en-us/library/2da4hk1d%28v=VS.100%29.aspx
-
10unlink is a Posix function. MS included many Posix functions in the C runtime headers for their compiler, but this polluted the namespace. To be more compliant with the C standard, MS later replaced some of the Posix functions they had provided with versions prefixed with an underscore (and removed others). Leading underscores are reserved to the implementation. In general, C runtime functions are more portable than Posix functions. Posix functions, in general, are pretty unix-centric, even though some non-unix OSes may provide some Posix support. Dec 2, 2011 at 1:00
-
7No, we do not agree. unlink is a Posix function. Posix was an attempt to standardize Unix-derived operating systems. _unlink is a different function that works on a non-Unix operating system. Jan 13, 2012 at 20:18
-
It may be a unix function, but you can define it so that if it finds unlink in the code when it's the windows OS, it actually goes to the definition of _unlink. I think that's what @bviktor was alluding to.– MicheleSep 19, 2016 at 12:05