So I have a textfile with 10 words with 5 characters each, and I want to get to the midpoint of that file without having to call read multiple times so I wanted to use lseek. As I understand, lseek only uses the SET, CUR, and END flags.
So If I have a textfile and all the words are only 5 words long...
I can use SEEK_SET to get the current word I'm at which would be 5
I can use SEEK_CUR to get to the next word location, so 10, then 15 and so on
I can use SEEK_END to get to 100.
My question is, if I want to get somewhere in between, say spot 76 or 24, or in my case 50, how can I do that? I tried doing using lseek with SEEK_SET / 2 and it doesn't work.
You seem to be confused as to what these flags do. From the man page:
So you would use
SEEK_SETto go to a specific location in the file,SEEK_CURto go to a location relative to the current location, andSEEK_ENDto go to a location relative to the end of the file.In your case, if you have 10 words of 5 characters each for a total of 50 bytes and want to go to the middle, you want to use
SEEK_SETwith an offset of 25. For example: