I am attempting to port a small data analysis program from a 64 bit UNIX to a 32 bit Windows XP system (don\'t ask :)). But now I am having problems with the 2GB file size l
sorry for not posting sooner but I have been preoccupied with other projects for a while. The following solution works:
__int64 nsamples(char* filename)
{
int fh;
__int64 n;
/* Open file */
fh = _open( filename, _O_BINARY );
/* Find end of file */
n = _lseeki64(fh, 0, SEEK_END);
/* Close file */
_close(fh);
return n / sizeof(short);
}
The trick was using _open
instead of fopen
to open the file.
I still don't understand exactly why this has to be done, but at least this works now.
Thanks to everyone for your suggestions which eventually pointed me in the right direction.
My BC says:
520093696 + 4294967296 => 4815060992
I'm guessing that your print routine is 32-bit. Your offset returned is most likely correct but being chopped off somewhere.
And for gcc, see SO question 1035657. Where the advice is compile with the flag -D_FILE_OFFSET_BITS=64 so that the hidden variable(s) (of type off_t) used by the f-move-around functions is(are) 64-bits.
For MinGW: "Large-file support (LFS) has been implemented by redefining the stat and seek functions and types to their 64-bits equivalents. For fseek and ftell, separate LFS versions, fseeko and ftello, based on fsetpos and fgetpos, are provided in LibGw32C." (reference). In recent versions of gcc, fseeko and ftello are built-in and a separate library is not needed.
There are two functions called _fseeki64 and _ftelli64 that support longer file offsets even on 32 bit Windows:
int _fseeki64(FILE *stream, __int64 offset, int origin);
__int64 _ftelli64(FILE *stream);