Listing

Back

Listing 16.8: Using the Function remove() to Delete a Disk File
Code  1:
      2:
      3:
      4:
      5:
      6:
      7:
      8:
      9:
     10:
     11:
     12:
     13:
     14:
     15:
     16:
     17:
     18:
     19:
/* LIST1608.c: Day 16 Listing 16.8 */
/* Demonstrates the remove() function. */
 
#include <stdio.h>
 
int main(void) {
  char filename[80];
  
  printf("Enter the filename to delete: ");
  gets(filename);
  
  if (remove(filename) == 0)
    printf("The file %s has been deleted.",
           filename);
  else
    fprintf(stderr, "Error deleting the file %s.",
            filename);
  return 0;
}
Output >list1608
Enter the filename to delete: *.bak
Error deleting the file *.bak.
 
>list1608
Enter the filename to delete: list1414.bak
The file list1414.bak has been deleted.
Description This program prompts the user on line 9 for the name of the file to be deleted. Line 12 then calls remove() to erase the entered file. If the return value is 0, the file was removed, and a message is displayed stating this. If the return value is not zero, an error occurred and the file was not removed.