C++ Standard Library C++ STL Library

C++ <cstdio> - remove() Function



The C++ <cstdio> remove() function is used to delete the file identified by character string pointed to by fname. If the file is currently open by the current or another process, the behavior of this function is implementation-defined.

Syntax

int remove ( const char * fname );

Parameters

fname Specify a pointer to a null-terminated string containing the name of the file to be deleted. The value shall include a path identifying the file to delete.

Return Value

Returns 0 if the file is successfully deleted, otherwise non-zero value is returned.
On most library implementations, the errno variable is also set to a system-specific error code on failure.

Example:

Lets assume that we have a file called test.txt. The example below shows how to delete this file using remove() function.

#include <cstdio>
 
int main (){
  int result;
  char fname[] = "test.txt";

  //trying to delete the file
  result = remove(fname);

  //displaying the message
  if(result == 0)
    printf("File is successfully deleted.");
  else
    printf("Some error has occurred.");
  
  return 0;
}

If the file exists at the given location and the program has write access to it, the file will be deleted and following message will be shown:

File is successfully deleted.

Otherwise, the file will not be deleted (if exists) and following message will be shown:

Some error has occurred.

❮ C++ <cstdio> Library