Working with files greater than 2GB on 32bit Linux
This applies to Linux, other Unix systems may need something slightly different.


The simplest (and recommended) way to deal with files >2GB on 32bit systems is to either define the macro _FILE_OFFSET_BITS to 64 or compile with -D_FILE_OFFSET_BITS=64

If you only care about running on 64bit none of this is an issue and can be ignored.


To write a program that will work on 64 and 32bit, define the _FILE_OFFSET_BITS macro.

/* gcc -o largefile largefile.c */
#define _FILE_OFFSET_BITS 64

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[])
        int fd;
        off_t size = 1024*1024*4000LL;
        const char *file = "/var/tmp/largefile_test";

        fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
        lseek(fd, size, SEEK_SET);
        write(fd, "TEST", 4);

Or instead of the #define you can compile with -D_FILE_OFFSET_BITS=64 e.g
$ gcc -D_FILE_OFFSET_BITS=64 -o largefile lagefile.c
This will switch various interfaces to their 64bit counterparts, e.g lseek() -> lseek64()


When using _FILE_OFFSET_BITS=64, the off_t data type as well some others becomes a 64bit signed integer, thus if you're using this in printf(), snprintf() etc, you should cast it to (long long) and this will do the right thing under both 64 and 32bit.