How to Close a File in C
A file must be close as soon as all operations on it have
been completed. This ensures that all outstanding information associated with
the file is flushed out from the buffers and all links to the file are broken.
It also prevents any accidental misuse of the file. In case, there is a limit
to the number of files that can be kept open simultaneously, closing of
unwanted files might help open the required files. Another instance where we
have to close a file is when we want to reopen the same file in a different
mode. The I/O library supports a function to do this for us. It takes the
following form:
This would close the file associated with the FILE pointer
file_pointer. Look at the folloing segment of a program.
……………
…………...
FILE *p1, *p2 ;
p1 = fopen ( “INPUT”, “w”) ;
p2 = fopen ( “OUTPUT”, “r”) ;
………….
………….
fclose (p1);
fclose (p2);
……………
Post a Comment