8

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?

1
  • 1
    A bunch of answers below say that close() flushes buffers to disk. However, close() is a POSIX function and no such thing is true of POSIX close(). It only releases locks and frees resources.
    – Jim Balter
    Jun 19, 2012 at 10:51

6 Answers 6

9
  • 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)

1
  • 6
    Several answers here claim that buffers are flushed upon close() but in fact that is not true ... POSIX close() does no flushing and has no bearing on whether or when data is written to the device.
    – Jim Balter
    Jun 19, 2012 at 11:04
3

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.

1
  • 1
    +1 Remarkably, this is the only answer here that does not erroneously assert that close() flushes data.
    – Jim Balter
    Jun 19, 2012 at 11:14
2

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.

0

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.

1
  • 4
    write() copies data from user space into system buffers. The system buffers will be flushed at the system's convenience or upon a sync(). close() has no bearing on the flushing of buffers. It only frees file locks and releases resources.
    – Jim Balter
    Jun 19, 2012 at 11:16
0

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/

2
  • 2
    Slight corrections. Your answer seems to indicate that files should be closed with fclose only. You can close files with close too. Its just that fclose takes in a file pointer and close takes a file descriptor. Also, many implementations flush the buffer when they encounter a newline character \n Jun 19, 2012 at 6:38
  • The question is about close(), not fclose(). Nothing you wrote here is actually true of close(). And Pavan's statement is perhaps even worse ... fopen pairs with fclose and open (or creat) pairs with close; the difference isn't just between file pointers and file descriptors, it's between quite different abstractions/APIs (one of which may happen to be implemented via the other).
    – Jim Balter
    Jun 19, 2012 at 11:12
0
  • 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.