Moving around inside a file with lseek()
In this recipe, we'll learn how to move around inside a file with
lseek(). This function operates on file descriptors, so please note that we are now working with file descriptors, not streams. With
lseek(), we can move around (or seek) freely inside a file descriptor. Doing so can be handy if we only want to read a specific part of a file or we want to go back and read some data twice and so on.
In this recipe, we will modify our previous program, called
fd-read.c, to specify where we want to start reading. We also make it so that the user can specify how many characters should be read from that position.
To easier understand this recipe, I encourage you to read the recipe named Reading from files with file descriptors in this chapter before reading this one.
How to do it…
The program we will write here will read a file using file descriptors. The user must also set a starting position where the read...