How does the fclose()
function work in C?
a) It closes an open file
b) It writes data to a file
c) It reads data from a file
d) It deletes a file
Answer:
a) It closes an open file
Explanation:
The fclose()
function in C is used to close an open file that was previously opened using fopen()
. It ensures that all data is written to the file and frees the resources associated with the file. After a file is closed, the FILE
pointer used to manage the file is no longer valid, and further operations on that pointer should be avoided.
Proper use of fclose()
is crucial for preventing memory leaks and ensuring that all file data is properly saved, especially in programs that handle multiple files or perform extensive file I/O operations.