Name
remove
Synopsis
Unlinks a file
#include <stdio.h> intremove
( const char *filename
);
The remove()
function
deletes the file (or directory) referred to by its string argument.
To be exact, it “unlinks” the file, or deletes its filename from the
file system, so that the file’s contents may still exist if the file
was linked to more than one name.
The remove()
function may
or may not be able to unlink a file while it is open, depending on
the given implementation. The function returns 0 on success. If
remove()
fails to unlink the
file, it returns a nonzero value.
Example
char fname_tmp[L_tmpnam] = "";
FILE *fp;
int result;
tmpnam( fname_tmp );
fp = fopen( fname_tmp, "w+" );
/* ... write something in the file, edit it ... */
fclose( fp );
result = rename( fname_tmp, "finished.txt" );
if ( result ) // Delete previous "finished.txt" and try again.
{remove
( "finished.txt" );
result = rename( fname_tmp, "finished.txt" );
if ( result ) // Give up and log the error.
fprintf( stderr, "Error %d on trying to rename output file\n", errno );
}
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.