If you use file streams instead of file descriptors, you can write yourself a (simple) function similar to the POSIX pread() system call.
You can easily emulate it using streams instead of file descriptors 1 . Perhaps you should write yourself a function (which has a slightly different interface from the one I suggested in the comment):
size_t fpread(void *buffer, size_t size, size_t mitems, size_t offset, FILE *fp) { if (fseek(fp, offset, SEEK_SET) != 0) return 0; return fread(buffer, size, nitems, fp); }
This is a reasonable compromise between the pread() and fread() conventions.
What would the function call syntax look like? For example, reading from offset 732, and then again from offset 432 (both located at the beginning of the file) and a stream stream called f .
Since you did not say how many bytes to read, I will take 100 every time. I assume that the target variables (buffers) are buffer1 and buffer2 , and that they are both large enough.
if (fpread(buffer1, 100, 1, 732, f) != 1) ...error reading at offset 732... if (fpread(buffer2, 100, 1, 432, f) != 1) ...error reading at offset 432...
The returned counter is the number of complete units of 100 bytes each; either 1 (got it all) or 0 (something went wrong).
There are other ways to write this code:
if (fpread(buffer1, sizeof(char), 100, 732, f) != 100) ...error reading at offset 732... if (fpread(buffer2, sizeof(char), 100, 432, f) != 100) ...error reading at offset 432...
This reads 100 single bytes each time; The test ensures that you get all 100 of them, as expected. If you take the return value in this second example, you can find out how much data you received. It would be very surprising if the first reading succeeded, and the second failed; some other program (or thread) would have to trim the file between two calls to fpread() , but, as you know, more funny things happened.
1 Emulation will not be perfect; calling pread() provides guaranteed atomicity that the combination of fseek() and fread() does not provide. But this will rarely be a problem in practice if you do not have several processes or threads updating the file at the same time while you are trying to arrange and read it.