remove


function
<cstdio>
int remove ( const char * filename );

Remove file

Deletes the file whose name is specified in filename.
This is an operation performed directly on a file; No streams are involved in the operation.

Parameters

filename
C string containing the name of the file to be deleted. This paramenter must follow the file name specifications of the running environment and can include a path if the system supports it.


Return value

If the file is successfully deleted, a zero value is returned.
On failure, a nonzero value is reurned and the errno variable is set to the corresponding error code. Error codes are numerical values representing the type of failure occurred. A string interpreting this value can be printed to the standard error stream by a call to perror.

Example

1
2
3
4
5
6
7
8
9
10
11
/* remove example: remove myfile.txt */
#include <stdio.h>

int main ()
{
  if( remove( "myfile.txt" ) != 0 )
    perror( "Error deleting file" );
  else
    puts( "File successfully deleted" );
  return 0;
}


If the file myfile.txt existed before the execution and we had write access to it, then the file will be deleted and this message will be written to stdout:
File successfully deleted

Otherwise, a message similar to this will be written to stderr:
Error deleting file: No such file or directory

See also