Home » C Programs » fclose() Function in C

fclose() Function in C

The fopen function opens a file and returns a pointer to structure FILE, a predefined structure in the “stdio.h” header file. If the file is successfully opened, it returns a reference to the file; if the file cannot be opened, it returns NULL. The fgetc method returns a character from the file, whereas the fclose function shuts it.

fclose() Function

On success, the fclose() function returns true; on failure, it returns false. It takes as an argument the file that needs to be closed and closes it. fopen() function in C for beginners to understand the function. Using this fclose() we have executed this fclose() function in c

stdlib.h in C Program

h is the header of the c programming language’s general purpose standard library, which includes memory allocation, process control, conversions, and other functions. It is C++ compatible and is known as cstdlib in C++. “stdlib” is an abbreviation for “standard library.”

Exit Function in C

The exit function in the c programming language calls all functions registered with at exit and terminates the program. Flushing file buffers, closing streams, and deleting temporary files are all performed. The exit function is defined in the stdlib. h header and is part of the c standard library.

fclose() Function in C

//fclose() function in c
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[50];
FILE *fp;
printf("Select the file\n");
gets(file_name);
fp = fopen(file_name, "r"); 
if (fp == NULL)
{
perror("File is not opening\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are\n", file_name);
while((ch = fgetc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
return 0;
}

Output 

Inlarn.txt

The content of inlarn.txt files are