close()
PROTOTYPE
#include <posix.h>
int close(int fid);
DESCRIPTION
close() frees the file control block identified by file descriptor fid. After close() returns, fid no longer references the file. If data was previously written to the file, some of it may still be in the volume’s data cache after close() returns. Closing a file does not flush its data to backing store.
To ensure all file changes (user data and metadata) are saved to backing store, either call fflush() or fsync() using the file descriptor or pointer before the file is closed, or call fflush(NULL), sync(), unmount(), or vsync() anytime.
On file systems that support links (RFS/XFS), if an open file is deleted, it can’t be reopened using its former path but deletion is delayed if the file has another link or is open via another handle. If the file being closed was previously deleted, has no other link, and no other open file handle, close() will both close the file and delete it.
If successful, close() returns 0. Otherwise, errno is set and -1 returned. If the file has unsaved changes and there is a preexisting fatal error when close() is called, it will set errno accordingly and return -1 but unless fid is invalid, the file control block is always freed.
ERROR CODES
| EBADF | fid is not the descriptor of an open file. |
| EBADFSYS | The file has unsaved changes and corruption has been detected. |
| EBITERR | The file has unsaved changes and error correction has failed. |
| EIO | The file has unsaved changes and a driver routine has failed. |
| ENOMEM | The file has unsaved changes and a critical memory allocation failed. |
| ENXIO | The file has unsaved changes and the volume has been removed. |
EXAMPLE
/*-------------------------------------------------------------*/
/* Close file and check for errors. */
/*-------------------------------------------------------------*/
if (close(fid))
perror("close() failed");