Wednesday, May 6, 2009

Introducing pread

There are many situations when you just want to read some data from a file starting with a given offset. The usual way of doing this is:
lseek(fd, offset, SEEK_SET);
read(fd, buffer, LEN);

You can do this in one function call using pread(2) . The above code is equivalent with:

pread(fd, buffer, LEN, offset);

In a similar manner you can notice the existence of pwrite(2) which writes some data into a file at a given position.