# Deleting a file in C

This topic is 4803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Umm, this is kind of embarasing, but so far, in my 3 years of C I never needed to delete a file. I think that the function is unlink() but I am not 100% sure (from what I read, it does some fancy things, such as decrementing the refference count and such). So, is unlink() the [only/right] way to delete a file? P.S. I need a generic C function, not windows or something.

##### Share on other sites
unlink is a POSIX function that generally isn't available on Windows (although VC++ does emulate it). I don't know if there is a general delete file function.

##### Share on other sites
Well, DevC++ (gcc) seems to know about it.
This is for an MMORPG server and it will run on Posix (FreeBSD), and on Windows for local testing.
If VC can also emulate it, the better (one developer has VC).

##### Share on other sites
remove

Quote:
 int remove ( const char * filename );Delete a file. Deletes the file specified by filename. It is compiled as a call to the system function for deleting files (unlink, erase or del).Parameters.filename Path and name of the file to be removed. Return Value. If the file is succesfully deleted a 0 value is returned. On error a non-zero value is returned and the errno variable is set with the corresponding error code that that can be printed with a call to perror:

Thanks :)

##### Share on other sites
remove() is not standard. unlink() is the POSIX standard -- the talk about "reference counts" is what "delete a file" really means on UNIX systems. Usually, it just removes the file (unless there are multiple hard links).

##### Share on other sites
But it said that it compiles to whatever delete function the system has.

##### Share on other sites
nprz is right. remove is an ANSI function (I just looked it up). That'll be safe to use.

##### Share on other sites
Quote:
 Original post by hplus0603remove() is not standard. unlink() is the POSIX standard -- the talk about "reference counts" is what "delete a file" really means on UNIX systems. Usually, it just removes the file (unless there are multiple hard links).

Now where'd you learn that? I have multiple sources that say remove is standard:

Quote:
 From subpages of http://www.unix.org/version3/apis.html, previously linked in this thread: POSIX P96Interface XSI Base U98 U95 P92 C99 C89 SVID3 BSD[...]remove() m m m m m m m m .[...]unlink() m m m m m . . m m[...]m Indicates that the interface is defined as mandatory.[...]. Indicates that the interface is not specified.

Quote:
 From panda@industry:~\$ man 3 remove:NAME remove - delete a name and possibly the file it refers toSYNOPSIS #include int remove(const char *pathname);[...]CONFORMING TO ANSI C, SVID, AT&T, POSIX, X/OPEN, BSD 4.3

##### Share on other sites
This book on ANSI C by Kernighan and Ritchie and Bell Labs (who wrote the language, I believe), has remove() as a standard library function.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631758
• Total Posts
3002137
×