remove() function in C language with Example

Here, we are going to learn about the remove() function of library function stdio.h in C language with its syntax, example.
Submitted by Souvik Saha, on February 28, 2019

remove() function in C

The remove() function is defined in the <stdio.h> header file.

Prototype:

    int remove(const char* filename);

Parameters: const char *filename

Return type: int

Use of function:

When we are dealing with files then sometimes we need to erase or delete some files. In file handling, we use remove() function to erase the files. The prototype of the function remove() is int remove(const char* filename);

Here, filename is the name of the file which has to be erased. It returns zero if the file is successfully deleted and non zero if an error occurred.

remove() example in C

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE* f;
    //Check the existence of that file
    if ((f = fopen("includehelp.txt", "r")) == NULL) {
        printf("File does not exist...\n");
    }
    else {
        printf("File is exist.\n");
    }
    fclose(f);

    //remove file
    if (remove("includehelp.txt"))
        printf("Remove error.....\n");
    else
        printf("File is removed\n");

    //Check the existence of that file
    if ((f = fopen("includehelp.txt", "r")) == NULL) {
        printf("File does not exist...\n");
    }
    else {
        printf("File is exist.\n");
    }

    fclose(f);

    return 0;
}

Output

remove() example in C language

C stdio.h Library Functions Programs »





Comments and Discussions!

Load comments ↻





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