unlink vs remove in c++

43,597

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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

Share:
43,597

Related videos on Youtube

SyBer
Author by

SyBer

Updated on September 15, 2020

Comments

  • SyBer
    SyBer almost 4 years

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

    • Martin York
      Martin York over 11 years
      I doubt you will notice any difference in speed. Making the choice will not be the costly part of the operation.
  • Adrian McCarthy
    Adrian McCarthy over 12 years
    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.
  • Adrian McCarthy
    Adrian McCarthy over 12 years
    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.
  • Keith Thompson
    Keith Thompson over 11 years
    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.
  • Michele
    Michele over 7 years
    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.