4

I'm working on some code that needs to run on every version of windows since WIN2000 and also needs to work with wide file paths.

I need to call some variant of stat to get the file length. The file may be larger than 4GB.

Here's the relevant section from the MSDN Visual Studio .NET 2003[1] documentation:


int _stat(
   const char *path,
   struct _stat *buffer 
);
int _stat64(
   const char *path,
   struct __stat64 *buffer 
);
int _stati64(
   const char *path,
   struct _stati64 *buffer 
);
int _wstat(
   const wchar_t *path,
   struct _stat *buffer 
);
int _wstat64(
   const wchar_t *path,
   struct __stat64 *buffer 
);
int _wstati64(
   const wchar_t *path,
   struct _stati64 *buffer 
);

[1] https://web.archive.org/web/20110506201149/http://msdn.microsoft.com/en-us/library/14h5k7ff(v=VS.71).aspx

I can't figure out the difference between the __stat64 structure and the _stati64 structure. I know that I want to use _wstat64 or _wstati64 but MSDN is silent on which is better.

Any suggestions?

AJM
  • 1,317
  • 2
  • 15
  • 30
vy32
  • 28,461
  • 37
  • 122
  • 246
  • And to add to the confusion, there's also [`GetFileSizeEx`](http://msdn.microsoft.com/en-us/library/aa364957%28v=vs.85%29.aspx). – Adam Rosenfield Jul 04 '11 at 03:19

3 Answers3

5

Here are the __stat64 and the _stati64 structures from the mingw wchar.h #include file:


#if defined (__MSVCRT__)
struct _stati64 {
    _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;
    time_t st_atime;
    time_t st_mtime;
    time_t st_ctime;
};

#if __MSVCRT_VERSION__ >= 0x0601
struct __stat64
{
    _dev_t st_dev;
    _ino_t st_ino;
    _mode_t 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;
};

According to these structures, it seems that _stat64 is a better choice than stati64 because:

  1. st_mode is _mode_t and not unsigned short
  2. Time is expressed as a _time64_t and not a time_t, so it has the same range that can be expressed by the NTFS file system, and is not crippled to the 32-bit time_t.

I'm still confused, but this seems closer to the correct answer.

Notice also that the _stat64 requires MSVCRT_VERSION > 0x0601, which implies that it is more modern.

vy32
  • 28,461
  • 37
  • 122
  • 246
3

I'm not 100% sure, but it seems like:

  • stat: 32-bit timestamp, 32-bit filesize
  • stat64 : 64-bit timestamp, 32-bit filesize
  • stati64: 64-bit timestamp, 64-bit filesize

So you would need wstati64.

This from the following paragraphs on MSDN:

The date stamp on a file can be represented if it is later than midnight, January 1, 1970, and before 19:14:07 January 18, 2038, UTC unless you use _stat64 or _wstat64, in which case the date can be represented up till 23:59:59, December 31, 3000, UTC.

and

st_size Size of the file in bytes; a 64-bit integer for _stati64 and _wstati64

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Okay. Looks like I need wstati64. It's still really confusing to me, though. – vy32 Jul 04 '11 at 03:35
  • Actually, see my answer below... (I would put it here but I can't put a
     format in a comment.
    – vy32 Jul 04 '11 at 17:27
  • Note that your summary has a mistake `stat64` has a 64-bit filesize which is shown at https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions?view=vs-2019 The difference to the `stati64` is that the latter has a timestamp depending on a define – Flamefire Jun 16 '20 at 16:32
  • @Flamefire: It says "Variations of these functions support 32- or 64-bit time types, and 32- or 64-bit file lengths. The first numerical suffix (32 or 64) indicates the size of the time type used; the second suffix is either i32 or i64, indicating whether the file size is represented as a 32-bit or 64-bit integer." and then it uses types without two suffixes. Poor documentation. Also as pointed out in the comments to the other answer, this part changed between VS2003 and VS2010. – Ben Voigt Jun 16 '20 at 18:28
  • Please check the link. There is a nice and explicit table. This might be recently added as they noticed their bad docu – Flamefire Jun 16 '20 at 19:53
1

The documentation says:

The first numerical suffix (32 or 64) indicates the size of the time type used; the second suffix is either i32 or i64, indicating whether the file size is represented as a 32-bit or 64-bit integer.

Community
  • 1
  • 1
user541686
  • 205,094
  • 128
  • 528
  • 886
  • That's confusing. it doesn't have two suffixes. It has either an i or no i, and a "" or a 64. – vy32 Jul 04 '11 at 03:33
  • 1
    @vy32: You're looking at the Visual Studio 2003 docs, Mehrdad is looking at the Visual Studio 2010 docs. – Ben Voigt Jul 04 '11 at 03:41
  • Ah. Well, the problem with the Visual Studio 2010 docs, I think, is that they assume you have a modern runtime. My program needs to be able to run on legacy Windows 2000 systems. – vy32 Jul 04 '11 at 04:12
  • The VS2010 docs refer to the VS2010 version of the CRT. You are right to use the VS2003 docs. Nonetheless, the principles of the naming are almost the same, and you want the wstati64 version. – Martyn Lovell Jul 04 '11 at 06:56