C Program to delete a file.

Write a C Program to delete a file.
To delete a file in c, We use the remove macro, which is defined in stdio.h. remove macro takes filename with extension as its argument and deletes the file, if it is in the directory and returns the zero, if deleted successfully.
Note that deleted file does not goes to the recycle bin, it is going to delete permanently.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#include<process.h>
main()
{
FILE *fp;
char filename[20];
int status;
clrscr();
printf("nEnter the file name:nn");
scanf("%s",filename);

fp = fopen(filename, r);

if(fp == NULL)
{
printf("Error: file not found! check the directory!!nn");
exit(0);
}
fclose(fp);
status = remove(file_name);

if( status == 0 )
printf("%s file deleted successfully.n",file_name);
else
{
printf("Unable to delete the filen");
perror("Error");
}

return 0;
}
Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

Leave a Reply