Deleting a file in C

Started by
8 comments, last by GameDev.net 18 years, 11 months ago
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.
Advertisement
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.
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).
Googled and found this:
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 :)
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).
enum Bool { True, False, FileNotFound };
But it said that it compiles to whatever delete function the system has.
nprz is right. remove is an ANSI function (I just looked it up). That'll be safe to use.
Quote:Original post by hplus0603
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).


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 <stdio.h>       int remove(const char *pathname);[...]CONFORMING TO       ANSI C, SVID, AT&T, POSIX, X/OPEN, BSD 4.3
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.

This topic is closed to new replies.

Advertisement