Shen
Shen

Reputation: 3169

Why do I have to use close() to close a file?

I'm learning some file functions and hence have a doubt.

I'm curious about why it is necessary to call close() to close a file? If I did not call close() after reading/writing a file, what things might happen? And if I did call close(),can I still use the file descriptor?

Upvotes: 8

Views: 11876

Answers (6)

Mark
Mark

Reputation: 1100

  • When you write into a file you write first into a buffer, some function call the flush and the data is written on to the file, but others just write into the buffer. When the buffer is full it flushes on to the file without any direct call for a flush. You can also flush whenever you want if you want to be sure.
  • And you cannot use the file descriptor anymore after you closed it.

Upvotes: 0

verisimilitude
verisimilitude

Reputation: 5108

The close() function closes the connection between the program and an open file identified by a handle. Any unwritten system buffers are flushed to the disk, and system resources used by the file are released

The bolded part is the prime reason why a file should be closed

Closing a file has the following consequences:

1)The file descriptor is deallocated.
2) Any record locks owned by the process on the file are unlocked.
3) When all file descriptors associated with a pipe or FIFO have been closed, any unread data is discarded.

Upvotes: 3

Hrishikesh
Hrishikesh

Reputation: 1183

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/

Upvotes: 0

Manik Sidana
Manik Sidana

Reputation: 2155

When you write to a file using write() system call, it doesn't writes immediately to the file. So after your operations, you have to call close() so that the buffer is flushed to the file and changes persist. After calling close(), you cannot use the file descriptor.

Upvotes: 0

John3136
John3136

Reputation: 29266

  • If the file has any sort of buffering behind it it and you don't call close then you could potentially lose data.

  • If the OS has limited resources (e.g. number of open files) then by not closing files you are wasting system resources.

  • Using a descriptor once the file is closed is pointless at best, massive bug at worst (a bit like using memory after it has been freed)

Upvotes: 9

Maksym Polshcha
Maksym Polshcha

Reputation: 18358

If you don't close a file opened for reading it can be blocked for concurrent writes. If you don't close a file opened for writing - you can loose a piece of last-written data which OS holds in the buffer.

And never use a descriptor of already closed file. This has no sense.

Upvotes: 2

Related Questions