Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

64-bit

stdio

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?

like image 933
gw0 Avatar asked Dec 21 '15 03:12

gw0


1 Answers

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().

like image 95
chux - Reinstate Monica Avatar answered Sep 22 '22 19:09

chux - Reinstate Monica