Before anyone complains of "duplicate", I've been checking SO quite thoroughly, but there seem to be no clean answer yet, although the question looks quite simple.
I'm looking for a portable C code, which is able to provide the size of a file, even if such a file is bigger than 4GB.
The usual method (fseek
, ftell
) works fine, as long as the file remains < 2GB. It's fairly well supported everywhere, so I'm trying to find something equivalent.
Unfortunately, the updated methods (fseeko
, ftello
) are not supported by all compilers. For example, MinGW miss it (and obviously MSVC). Furthermore, some comments make me believe that the new return type (off_t) does not necessarily support size > 2GB, it may depend on some external parameters, to be checked.
The unambiguous methods (fseeko64
, ftello64
) are not supported by MSVC. MS provides their equivalent, _fseeki64
& _ftelli64
. This is already bad, but it becomes worse : some Linux configurations seem to badly support these functions during run time. For example, my Debian Squeeze on PowerPC, using GCC 4.4, will produce a "filesize" method using fseeko64
which always return 0 (while it works fine for Ubuntu64). MinGW seems to answer some random garbage above 2GB.
Well, I'm a bit clueless as far as portability is concerned. And if I need to make a bunch of #if #else
, then why not go straight to the OS & compilers specifics methods in the first place, such as GetFileSize()
for MSVC for example.
I have implemented and tested the following:
using MinGW64 gcc compiler 4.8.1 and Linux gcc 4.6.3 compiles and works.
On OSX, no redefinition of stat required.
for lstat and fstat functions I expect similar macro #defines to work.