Search code examples
winapifseekftell

fast access into large file - fseek ftell fsetpos fgetpos


I'm faced with providing some sort of rapid access to selected data in gigabyte files. Once I locate the starting point, subsequent access would be sequential. The files include a date at the front of each record and the date increases throughout the file. I don't want to start at the beginning and read through the file ignoring stuff at the front. I'm ONLY interested in WINDOWS applications. I'd like to open the file in text mode.

It occurs to me that I could do some sort binary search using the 64 bit fseek/ftell or the fsetpos/fgetpos functions. I realize I might seek into the middle of a newline delimited record but one would think you could simply skip any partial record with a fgets call.

fseek offers the origin SEEK_END mechanism so it seems that could be used with a ftell to determine file size. I don't see a similar facility with fsetpos/fgetpos. How would one get the file size if you went with fsetpos/fgetpos?

_fseeki64/_ftelli64 use int64 and fpos_t used by fsetpos/fgetpos also is int64 so I should be able to do arithmetic on the pointers.

Or am I off on the wrong track here? Should I consider some other approach?


Solution

  • _fseeki64/_ftelli64 do exactly what you want, so why do you hesitate to use them? Your approach is fine.