C program to delete a file
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()
- This function takes a string as a parameter, which represents the name of the file to be deleted.
- The function returns 0 if the file is deleted successfully, Otherwise, it returns a non-zero value.
Examples of remove()
The below C program demonstrates the use of remove() function.
) == 0)
"Unable to delete the file"
If file deleted successfully
If file not deleted successfully
Unable to delete the file
Using remove() function in C, we can write a program that can destroy itself after it is compiled and executed.
"By the time you will compile me I will be "
By the time you will compile me I will be destroyed
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.
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!