Skip to content
Home » C Programs » fclose() Function in C Example

fclose() Function in C Example

Basic program to 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 in C

The fclose() function in PHP is a built-in function that is used to close a file pointed to by an open file pointer. 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 of fclose() function in c:

Inlarn.txt

The content of inlarn.txt files are