The Wayback Machine - https://web.archive.org/web/20240118180953/https://www.geeksforgeeks.org/c-program-delete-file/
Open In App
Related Articles

C program to delete a file

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The remove() function in C/C++ can be used to delete a file. The function returns 0 if the file is deleted successfully, Otherwise, it returns a non-zero value. The remove() is defined inside the <stdio.h> header file.

Syntax of remove()

remove("filename");

Parameters

  • This function takes a string as a parameter, which represents the name of the file to be deleted.

Return Value

  • The function returns 0 if the file is deleted successfully, Otherwise, it returns a non-zero value.

Examples of remove()

Example 1:

The below C program demonstrates the use of remove() function.

C




// C program that demonstrates
// the use of remove() function
#include <stdio.h>
 
int main()
{
    if (remove("abc.txt") == 0)
        printf("Deleted successfully");
    else
        printf("Unable to delete the file");
 
    return 0;
}


Output

If file deleted successfully
Deleted successfully
            OR
If file not deleted successfully
Unable to delete the file

Example 2:

Using remove() function in C, we can write a program that can destroy itself after it is compiled and executed.

C




// C program that can destroyitself
//after it is compiled and executed
//using remove() function.
#include <stdio.h>
#include <stdlib.h>
 
int main(int c, char* argv[])
{
    printf("By the time you will compile me I will be "
           "destroyed \n");
 
    // array of pointers to command line arguments
    remove(argv[0]);
 
    // Note: argv[0] will contain the executable file i.e.
    // 'a.out'
 
    return 0;
}
 
// This code is contributed by MAZHAR IMAM KHAN.


Output

By the time you will compile me I will be destroyed

Explanation

Note that, this is done in the Linux environment. The remove function is fed the first parameter in the command line argument i.e. a.out file (executable file) created after compiling. Hence the program will be destroyed.

Note: After the output shown above, the a.out file will be removed.


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 06 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads