How to remove or delete a file in C ?
To remove a file in C , you can use the remove
function from the stdio
header .
This functions takes the path to the file that should be removed , and it returns 0
on success , and a non zero value on failure . If the file is open , then what happens is implementation defined .
Under unix , more than one link can point to the same file , in this case only one of the links is removed , and not the actual file . Also under unix , the remove
function will remove an empty directory , but not a directory containing data .
Under windows trying to remove a directory using this function will cause an error .
The remove
function sets errno
, which contains an error code describing the actual error . A textual description , of the errno
error code , can be gotten using the perror
function of stdio
.
An example of a program to remove a file using the C standard library is :