Reference
Opening a file: open()
- The flags argument
- Errors from
open()
: If an error occurs while trying to open the file, open() returns –1, and errno identifies the cause of the errorEACCES The file permissions don’t allow the calling process to open the file in the mode specified by flags. Alternatively, because of directory permissions, the file could not be accessed, or the file did not exist and could not be created. EISDIR The specified file is a directory, and the caller attempted to open it for writing. This isn’t allowed. EMFILE The process resource limit on the number of open file descriptors has been reached ENFILE The system-wide limit on the number of open files has been reached. ENOENT The specified file doesn’t exist, and O_CREAT was not specified, or O_CREAT was specified, and one of the directories in pathname doesn’t exist or is a symbolic link pointing to a nonexistent pathname (a dangling link). EROFS The specified file is on a read-only file system and the caller tried to open it for writing. ETXTBSY The specified file is an executable file (a program) that is currently executing. It is not permitted to modify (i.e., open for writing) the executable file associated with a running program. (We must first terminate the program in order to be able to modify the executable file.)
Reading from a file: read()
- The
read()
system call reads data from the open file referred to be the descriptorfd
.
count
argument: specifies the maximum number of bytes to read.buffer
argument: specifies the address of the memory buffer into which the input data is to be placed (must be at leastcount
bytes long).
Closing a file: close()
- The close() system call closes an open file descriptor, freeing it for subsequent reuse by the process. When a process terminates, all of its open file descriptors are automatically closed.
Changing the file offset: lseek()
- For each open file, the kernel records a file offset - This is the location in the file at which the next
read()
orwrite()
will commence. - The file offset is set to point to the start of the file when the file is opened and is automatically adjusted by each subsequent call to read() or write() so that it points to the next byte of the file after the byte(s) just read or written
- The lseek() system call adjusts the file offset of the open file referred to by the
file descriptor fd, according to the values specified in
offset
andwhence
. - Calling lseek() simply adjusts the kernel’s record of the file offset associated with a file descriptor. It does not cause any physical device access.
- We can’t apply lseek() to all types of files.
Arguments
offset
: specifies a value in byteswhence
: base point from whichoffset
is to be interpretedValue Usage SEEK_SET The file offset is set offset bytes from the beginning of the file. SEEK_CUR The file offset is adjusted by offset bytes relative to the current file offset. SEEK_END The file offset is set to the size of the file plus offset. In other words, offset is interpreted with respect to the next byte after the last byte of the file.
Examples
Note
- Writing data at a position beyond the previous end of the file creates a hole in the file. Reads from a file hole return bytes containing zeros.