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?

phuclv
  • 30,396
  • 15
  • 136
  • 260

1 Answers1

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.

phuclv
  • 30,396
  • 15
  • 136
  • 260
harrymc
  • 498,455