[ACCEPTED]-Seeking and reading large files in a Linux C++ application-large-files

Accepted answer
Score: 27

fseek64 is a C function. To make it available 7 you'll have to define _FILE_OFFSET_BITS=64 6 before including the system headers That 5 will more or less define fseek to be actually 4 fseek64. Or do it in the compiler arguments 3 e.g. gcc -D_FILE_OFFSET_BITS=64 ....

http://www.suse.de/~aj/linux_lfs.html has 2 a great overviw of large file support on 1 linux:

  • Compile your programs with "gcc -D_FILE_OFFSET_BITS=64". This forces all file access calls to use the 64 bit variants. Several types change also, e.g. off_t becomes off64_t. It's therefore important to always use the correct types and to not use e.g. int instead of off_t. For portability with other platforms you should use getconf LFS_CFLAGS which will return -D_FILE_OFFSET_BITS=64 on Linux platforms but might return something else on e.g. Solaris. For linking, you should use the link flags that are reported via getconf LFS_LDFLAGS. On Linux systems, you do not need special link flags.
  • Define _LARGEFILE_SOURCE and _LARGEFILE64_SOURCE. With these defines you can use the LFS functions like open64 directly.
  • Use the O_LARGEFILE flag with open to operate on large files.
Score: 10

If you want to stick to ISO C standard interfaces, use 23 fgetpos() and fsetpos(). However, these functions are only 22 useful for saving a file position and going 21 back to the same position later. They represent 20 the position using the type fpos_t, which is not 19 required to be an integer data type. For 18 example, on a record-based system it could 17 be a struct containing a record number and 16 offset within the record. This may be too 15 limiting.

POSIX defines the functions ftello() and 14 fseeko(), which represent the position using the 13 off_t type. This is required to be an integer 12 type, and the value is a byte offset from 11 the beginning of the file. You can perform 10 arithmetic on it, and can use fseeko() to perform 9 relative seeks. This will work on Linux 8 and other POSIX systems.

In addition, compile 7 with -D_FILE_OFFSET_BITS=64 (Linux/Solaris). This will define 6 off_t to be a 64-bit type (i.e. off64_t) instead of 5 long, and will redefine the functions that use 4 file offsets to be the versions that take 3 64-bit offsets. This is the default when 2 you are compiling for 64-bit, so is not 1 needed in that case.

Score: 5

fseek64() isn't standard, the compiler docs should 4 tell you where to find it.

Have you tried 3 fgetpos and fsetpos? They're designed for large files 2 and the implementation typically uses a 1 64-bit type as the base for fpos_t.

Score: 5

Have you tried fseeko() with the _FILE_OFFSET_BITS preprocessor symbol 4 set to 64?

This will give you an fseek()-like interface 3 but with an offset parameter of type off_t instead 2 of long. Setting _FILE_OFFSET_BITS=64 will make off_t a 64-bit type.

The 1 same for goes for ftello().

Score: 2

Use fsetpos(3) and fgetpos(3). They use the fpos_t datatype , which 2 I believe is guaranteed to be able to hold 1 at least 64 bits.

More Related questions