C Program to Delete a File

In this tutorial, you will learn and get code about how to delete a file from the current directory using a C program. But before going through the program, we have to do few things.

Things to do Before Program

Before creating a C program to delete a file. Let's first create a file in the directory where the C source code is saved. Because I'm saving my source code file to a folder named c programs inside the directory of Documents of my computer system. Therefore here is the snapshot of the folder, that contains a file named codescracker.txt

c delete file

You can also create a file in the directory where you are saving your C programs. Because after running the program, we have to enter the name of file to be delete. And at that time, I'll enter the name of this file, codescracker.txt. Now let's move on to the program.

Delete a File from Current Directory

To delete a file, use a function named remove() that takes string as parameter. Here the string parameter is the name of file (with extension) that has to be delete. This function returns 0 if file gets deleted. Here is the program, that is used to delete any file present inside the current directory:

int main()
    int status;
    char fname[20];
    printf("Enter FileName: ");
    status = remove(fname);
        printf("\nFile deleted successful!");
        printf("\nUnable to delete file!");
    return 0;

This program was build and run under Code::Blocks IDE. Here is its sample run:

c program to delete files

Now supply or enter the name of file say codescracker.txt and press ENTER key to delete this file. Here is the sample run:

delete a file in c

Now if you want to see the snapshot after the operation, then here is the snapshot of current directory (snapshot of c programs folder as created earlier):

c program delete file from directory

Same Program in Other Languages

C Online Test

« Previous Program Next Program »

Like/Share Us on Facebook 😋