Closing the File in C

Once we open a file, finished reading from the file, we need to close the file. This is done using the function fclose() through the statement:

fclose(fp);

Once we are done with closing the file, we can no longer read, write or perform any operation on the file unless we reopen it.  Note that to close the file, we don’t use the filename but the file pointer fp. On closing the file, the buffer associated with the file is removed from memory.

While performing a write operation on a file, when we try to close the file using fclose(), three operations would be performed:

  1. The character in the buffer would be written to the file on the disk
  2. At the end of the file a character with ASCII value 26 would be written
  3. The buffer would be eliminated from memory

You may also like...

2 Responses

  1. Avinash says:

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet