C program to delete a specified file using remove() function

Here, we are going to learn how to delete a specified file using remove() function in C language? By Nidhi Last updated : March 10, 2024

Problem statement

In this program, we will delete a specified file using the remove() function. The remove() function returns 0, if file deleted successfully.

C program to delete a specified file

The source code to delete a specified file using the remove() function is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to delete a specified file
// using remove() function

#include <stdio.h>

int main()
{
    char fileName[] = "includehelp.txt";
    int ret = 0;

    ret = remove(fileName);

    if (ret == 0)
        printf("File deleted successfully\n");
    else
        printf("Unable to delete file %s\n", fileName);

    return 0;
}

Output

File deleted successfully

Explanation

In the main() function, we created a character array filename, which is initialized with "inclludehelp.txt". Then we used the remove() function to remove the specified file. The function returns the 0, if the file deleted successfully, otherwise it will return a non-zero value. After that, we printed the appropriate message on the console screen.

C File Handling Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.