53

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. Commented Feb 28, 2013 at 0:05

5 Answers 5

70

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.

Sign up to request clarification or add additional context in comments.

Comments

25

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

Comments

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 Comment

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.
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 Comments

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.
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.
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.
-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.