Search code examples
c64-bitstdio

What is the best way to seek past 2GiB in C?


There seem to be many different ways to seek in C:

  • fseek()
  • fsetpos()
  • fseeko()
  • lseek()

And many seem to have *64() versions:

  • fseeko64()
  • lseek64()

To complicate matters further many seem to require macro definitions (like _LARGEFILE64_SOURCE or _GNU_SOURCE) to be available or to use 64-bit versions.

What is the easiest way to guarantee 64-bit IO using ANSI C on Windows, Linux, Mac, BSD, Solaris, etc., and since when has it been supported by each OS?


Solution

  • Code works needs to employ at least 1 of the below restriction::

    1. long is 64-bit+, then use fseek().

    2. Only seek to locations that code has been to before as a result of fread(), fgets(), fscanf(), fgets(), and so use fgetpos(), fsetpos(). This is the best way if the file is a text file.

    3. Have access to a platform dependent 64-bit seek such as lseek64().

    4. Multiple 32-bit fseek().

    5. Use grossly inefficient code with rewind() and fgetc()/fread().