Deleting a file using remove() C function in C program

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows Xp Pro with SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: The deleted file cannot be recovered

To do: Deleting a file using the remove() C function

To show: How to use the remove() C function to delete a given file

 

// Demonstrate the remove() function

#include <stdio.h>

void main(void)

{

// declare an array to store file name...

char filename[80];

 

printf("Enter the filename to be deleted: ");

// gets(filename);

gets_s(filename, 80);

 

// check any error...

if(remove(filename) == 0)

printf("File %s has been deleted.\n", filename);

else

fprintf(stderr, "Error deleting file %s.\n", filename);

}

 

Output example:

 

Enter the filename to be deleted: C:\testfour.txt

File C:\testfour.txt has been deleted.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index