I am running into integer overflow using the standard ftell and fseek options inside of G++, but I guess I was mistaken because it seems that ftell64 and fseek64 are not available. I have been searching and many websites seem to reference using lseek with the off64_t datatype, but I have not found any examples referencing something equal to fseek. Right now the files that I am reading in are 16GB+ CSV files with the expectation of at least double that.
Without any external libraries what is the most straightforward method for achieving a similar structure as with the fseek/ftell pair? My application right now works using the standard GCC/G++ libraries for 4.x.
fseek64()
isn't standard, the compiler docs should tell you where to find it.Have you tried
fgetpos
andfsetpos
? They're designed for large files and the implementation typically uses a 64-bit type as the base for fpos_t.Use
fsetpos(3)
andfgetpos(3)
. They use thefpos_t
datatype , which I believe is guaranteed to be able to hold at least 64 bits.Have you tried fseeko() with the _FILE_OFFSET_BITS preprocessor symbol set to 64?
This will give you an fseek()-like interface but with an offset parameter of type off_t instead of long. Setting _FILE_OFFSET_BITS=64 will make off_t a 64-bit type.
The same for goes for ftello().
fseek64 is a C function. To make it available you'll have to define _FILE_OFFSET_BITS=64 before including the system headers That will more or less define fseek to be actually fseek64. Or do it in the compiler arguments e.g. gcc -D_FILE_OFFSET_BITS=64 ....
http://www.suse.de/~aj/linux_lfs.html has a great overviw of large file support on linux:
If you want to stick to ISO C standard interfaces, use
fgetpos()
andfsetpos()
. However, these functions are only useful for saving a file position and going back to the same position later. They represent the position using the typefpos_t
, which is not required to be an integer data type. For example, on a record-based system it could be a struct containing a record number and offset within the record. This may be too limiting.POSIX defines the functions
ftello()
andfseeko()
, which represent the position using theoff_t
type. This is required to be an integer type, and the value is a byte offset from the beginning of the file. You can perform arithmetic on it, and can usefseeko()
to perform relative seeks. This will work on Linux and other POSIX systems.In addition, compile with
-D_FILE_OFFSET_BITS=64
(Linux/Solaris). This will defineoff_t
to be a 64-bit type (i.e.off64_t
) instead oflong
, and will redefine the functions that use file offsets to be the versions that take 64-bit offsets. This is the default when you are compiling for 64-bit, so is not needed in that case.