fclose() Function
The fclose()
function in C closes an open file stream, ensuring that any buffered data is flushed and that the file is properly disassociated from the stream. This is a crucial step in managing file resources and preventing data loss or corruption.
Syntax of fclose()
int fclose(FILE *stream);
Parameters
Parameter | Description |
---|---|
stream | A pointer to a FILE object that identifies the stream to be closed. |
When fclose()
is called, all internal buffers associated with the stream are flushed. Any unwritten output is written to the file, and any unread input is discarded. Even if the function call fails, the stream is disassociated from the file and its buffers.
Return Value
If the stream is successfully closed, fclose()
returns 0. On failure, it returns EOF
.
Examples for fclose()
Example 1: Basic File Closure
This example demonstrates how to open a file, close it using fclose()
, and check for successful closure.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "w");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
// File operations can be performed here
// Close the file and check the return value
if (fclose(fp) == 0) {
printf("File closed successfully.\n");
} else {
printf("Error closing file.\n");
}
return 0;
}
Explanation:
- The program opens a file named
example.txt
in write mode. - It checks if the file was successfully opened.
- After performing any file operations (omitted here for simplicity),
fclose()
is called to close the file. - The return value of
fclose()
is checked to determine if the file was closed successfully.
Program Output:
File closed successfully.
Example 2: File Closure After Writing Data
This example shows how to write data to a file and then properly close the file to ensure that all buffered data is flushed.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
// Write data to the file
fprintf(fp, "Hello, World!");
// Close the file and check if it closed successfully
if (fclose(fp) == 0) {
printf("Data written and file closed successfully.\n");
} else {
printf("Error closing the file.\n");
}
return 0;
}
Explanation:
- The file
data.txt
is opened in write mode. - Data (“Hello, World!”) is written to the file using
fprintf()
. - The file is then closed using
fclose()
, ensuring that the data is flushed to disk. - The return value is checked to confirm that the file closure was successful.
Program Output:
Data written and file closed successfully.
Example 3: Error Handling in File Closure
This example illustrates how to handle errors that might occur during the file closure process.
Program
#include <stdio.h>
int main() {
// Intentionally set file pointer to NULL to simulate error scenario
FILE *fp = NULL;
// Attempt to close an invalid file stream
if (fclose(fp) == EOF) {
printf("Error: Unable to close the file stream.\n");
} else {
printf("File stream closed successfully (unexpected).\n");
}
return 0;
}
Explanation:
- An invalid file pointer (
NULL
) is used to simulate an error condition. - The program attempts to close the file stream with
fclose()
. - Since the file pointer is invalid,
fclose()
returnsEOF
, triggering the error message.
Program Output:
Error: Unable to close the file stream.