5

According to this wikipedia page, the size of a individual file on Win10 can go up to about 8 PB with NTFS. Yet the struct Stat is defined as

struct stat
{
    _dev_t         st_dev;
    _ino_t         st_ino;
    unsigned short st_mode;
    short          st_nlink;
    short          st_uid;
    short          st_gid;
    _dev_t         st_rdev;
    _off_t         st_size;
    time_t         st_atime;
    time_t         st_mtime;
    time_t         st_ctime;
};

where off_t is type defined as long. That means a roughly 4 GB limit. According to this stackoverflow page, even for 32-bit OS, the size of a file can be larger than it.

Unless I have mistaken something, how can one use struct stat to get the size of a file reliably. Furthermore, what will happen if the file size exceeds the limit of the long type?

5

You should rather use the 64-bit versions of the stat functions.

The __stat64 structure from the stat.h include file:

struct _stat64
{
    _dev_t         st_dev;
    _ino_t         st_ino;
    unsigned short st_mode;
    short          st_nlink;
    short          st_uid;
    short          st_gid;
    _dev_t         st_rdev;
    __int64        st_size;
    __time64_t     st_atime;
    __time64_t     st_mtime;
    __time64_t     st_ctime;
};

In here st_size is __int64 which is 64-bit and not 32-bit.

3
  • Thanks. I thought __stat64 is for 64-bit OS. – John Z. Li Oct 10 '18 at 14:18
  • 1
    If you are on a 32-bit system you can still use the GetFileSize function. – harrymc Oct 10 '18 at 14:23
  • That is exactly I am looking for – John Z. Li Oct 10 '18 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.