50

What is the difference between remove and unlink functions in C++?

1
  • I doubt you will notice any difference in speed. Making the choice will not be the costly part of the operation. Feb 28, 2013 at 0:05

5 Answers 5

67

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.

0
22

remove is portable, and unlink is Unix-specific. :-P

8

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 ;)

1
  • 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
5

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

3
  • 10
    unlink 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
  • 7
    No, 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.
    – Michele
    Sep 19, 2016 at 12:05
0

remove() is part of the C++ standard (N4860 29.11.14.30). unlink() is not.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.