The end of Chapter 4
- lseek - go to a position in a file.
- Needs sys/types.h and unistd.h
- off_t lseek(int fd, off_t offset, int whence);
- whence is one of three values
- SEEK_SET seek offset bytes from the beginning of the file.
- offset must be positive.
- SEEK_CUR seek offset bytes from the current position.
- offset can be positive or negative, but the final position must not be less than 0.
- SEEK_END seek offset bytes from the end of the file.
- offset can be positive or negative, but the final position must not be less than 0.
- If a write occurs past the end of the file, a hole will be created in the file.
- Bytes in a hole are reported as 0.
- Disk blocks are not allocated.
- Some file systems do not support file holes.
- Newer versions of linux support the following whence values
- SEEK_DATA : adjust the offset to the next location in the file containing data.
- SEEK_HOLE : adjust the offset to the next hole.
- Both return -1 if they fail.
- The return value is the location measured in bytes on success or -1 on failure.
- lseek will not work on some file types.