remove() — Delete File

Format

#include <stdio.h>
int remove(const char *filename);

Language Level

ANSI

Threadsafe

Yes

Description

The remove() function deletes the file specified by filename. If the filename contains the member name, the member is removed or the file is deleted.
Note: You cannot remove a nonexistent file or a file that is open.

Return Value

The remove() function returns 0 if it successfully deletes the file. A nonzero return value indicates an error.

The value of errno may be set to ECONVERT (conversion error).

Example

When you call this example with a file name, the program attempts to remove that file. It issues a message if an error occurs.
#include <stdio.h>
 
int main(int argc, char ** argv)
{
  if ( argc != 2 )
    printf( "Usage: %s fn\n", argv[0] );
  else
    if ( remove( argv[1] ) != 0 )
      perror( "Could not remove file" );
}

Related Information