Lseek
Objectives
We would like to :
- Gain a basic understaing of the
lseek
system call.
Notes
-
off_t lseek(int fd, off_t offset, int whence);
- Requires unistd.h
- Takes a file descriptor to an open file.
- An ammount to move by
- A type of movement to do whence
-
SEEK_SET
set to this byte from the begiining
-
SEEK_CUR
Move by this amount relative to the current position
-
SEEK_END
move to the end of the file + offset
- But in mordern linux
-
SEEK_DATA
seeks the next byte of data in the file
-
SEEK_HOLE
seeks the enxt hole in the file.
- Returns
- The position in a file on success
- -1 on error.
- File holes: By seeking past the end of the file then writing a hole is produced in the file.
- From the book:
-
lseek(fd,0, SEEK_SET)
will go to the first byte
-
lseek(fd,0, SEEK_END)
will go to one byte past the end of the file.
-
lseek(fd, -1, SEEK_END)
will go to the last byte in the file.
-
lseek(fd, -10, SEEK_CUR)
will back up 10 bytes, but you can not seek before the beginning of a file.
- dice.cpp is a bad example of how to use this.
- seeker.cpp is a different example.