remove C Library Function

The function int remove(const char *filename); deletes the file whose name is passed in filename argument. It directly deletes the file identified by the filename argument without any stream operation.

Function prototype of remove

int remove(const char *filename);
  • filename : This is a null terminated string containing the name of the file to be deleted.

Return value of remove

On Success, remove function returns zero otherwise in case of an error it returns -1 and sets errno variable to a system-specific error code.

C program using remove function

The following program shows the use of remove function to delete a file. Below program first creates a file and then deletes it using remove function.

remove C Library Function

#include <stdio.h>
 
int main (){
   int response;
   FILE *file;
   char *fileName = "textFile.txt";
   /* Create a new file */
   file = fopen(fileName, "w");
   fputs("TechCrashCourse.com", file);
   fclose(file);
    
   /* Delete textFile.txt*/
   response = remove(fileName);
 
   if(response == 0) {
      printf("textFile.txt Deleted\n");
   } else {
      printf("Error: unable to delete textFile.txt");
   }
    
   return(0);
}

Output

textFile.txt Deleted