You are here

C program to delete a file

C program to delete a file whose name a user will input, the file to be deleted must be present in the directory in which the executable file of this program is present. Extension of the file should also be entered, remove macro is used to delete the file. If there is an error in deleting the file, then the error will be displayed by perror function.

C programming code

  1. #include <stdio.h>
  3. int main()
  4. {
  5.   int status;
  6.   char file_name[25];
  8.   printf("Enter name of a file you wish to delete\n");
  9.   gets(file_name);
  11.   status = remove(file_name);
  13.   if (status == 0)
  14.     printf("%s file deleted successfully.\n", file_name);
  15.   else
  16.   {
  17.     printf("Unable to delete the file\n");
  18.     perror("Following error occurred");
  19.   }
  21.   return 0;
  22. }

Download Delete file program executable.

Output of program:
Output of C program to delete a file

The deleted file doesn't go to trash or recycle bin, so you may not be able to recover it. Deleted files can be recovered using specialized recovery software, if they aren't overwritten on the storage medium.