Closing a file:
When done with a file, it must be closed using the function fclose().
fclose(ifp);
fclose(ofp);
Closing a file is very important, especially with output files. The reason is that output is often buffered. This means that when you tell C to write something out, e.g.,
fprintf(ofp, "Whatever!\n");
it doesn't necessary get written to disk right away, but may end up in a buffer in memory. This output buffer would hold the text temporarily:
Sample output buffer:
----------------------------------------------
| a | b | c | W | h | a | t | e | v | e | r |
----------------------------------------------
| ! | \n | | | | | | | | | |
----------------------------------------------
| | | | | | | | | | | |
----------------------------------------------
| | | | | | | | | | | |
----------------------------------------------
...
(The buffer is really just 1-dimensional despite this drawing.)
When the buffer fills up (or when the file is closed), the data is finally written to disk.
Reference: http://www.cs.bu.edu/teaching/c/file-io/intro/