C++ Remove() Function

C++ Remove() Function


    The remove() function in C++ deletes a specified file.

    Syntax :
int remove(const char* filename);
   The remove() function takes a single argument filename and returns an integer value. It deletes the         file pointed by the parameter.

   Incase the file to be deleted is opened by a process, the behaviour of remove() function is                       implementation-defined.

   It is defined in <cstdio> header file.

   remove() Parameters

  filename: Pointer to the string containing the name of the file along with the path to delete.

   remove() Return value

    The remove() function returns:

    1)  Zero if the file is successfully deleted.

     2) Non zero if error occurs.

   Example: How remove() function works

  #include <iostream>
#include <cstdio>

using namespace std;

int main()
char filename[] = "C:Users ile.txt";

/* Deletes the file if exists */
if (remove(filename) != 0)
perror("File deletion failed");
cout << "File deleted successfully";

return 0;

    When you run the program, the output will be:

If the file is deleted successfully:
File deleted successfully
If the file is not present:
File deletion failed: No such file or directory