Manipulating the current offset
Read and write operations can access a file sequentially because the current I/O offset of the file tracks the byte offset of each previous operation. The offset is stored in the system file table.
You can adjust the offset on files that can be randomly accessed, such as regular and special-type files, using the lseek subroutine.
| Subroutine | Description |
|---|---|
| lseek | Allows a process to position the offset at a designated byte.
The lseek subroutine positions the pointer at the byte designated
by the Offset variable. The Offset value can be calculated
from the following places in the file (designated by the value of
the Whence variable):
|
The return value for the lseek subroutine is the current
value of the pointer's position in the file. For example:
cur_off= lseek(fd, 0, SEEK_CUR);The lseek subroutine is implemented in the file table. All subsequent read and write operations use the new position of the offset as their starting location.
Note: The offset cannot be changed on pipes or socket-type files.
| Subroutine | Description |
|---|---|
| fclear | Subroutine that creates an empty space in a file. It sets to zero the number of bytes designated in the NumberOfBytes variable beginning at the current offset. The fclear subroutine cannot be used if the O_DEFER flag was set at the time the file was opened. |