0

In order to reliably get the size of a file on Windows, I have found two solutions on SO: GetFileSize or _stat64.

What are the pros and cons for each one?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • Not a duplicate, but see also: https://stackoverflow.com/questions/54557875/is-there-a-well-defined-platform-independent-way-to-get-the-size-in-bytes-of – Govind Parmar Nov 01 '19 at 14:49
  • You should show the links to the two solutions you mentioned. – Jabberwocky Nov 01 '19 at 15:03
  • 1
    what you mean under *reliably* ? `GetFileSize` winapi function which return you file size as is. or possible use `GetFileInformationByHandleEx` with `FileStandardInfo` or `ZwQueryInformationFile` with `FileStandardInformation` (`GetFileSize` internal do this). the `_stat64` much worse. only minus compare this. but internal in all way we call `ZwQueryInformationFile` with `FileStandardInformation` – RbMm Nov 01 '19 at 15:03
  • 2
    stat() is a posix function. Wrong OS, but supported by the Microsoft CRT, posix was once one of the api layers supported by Windows NT. Advantage is that you don't have to #include the large Windows.h. Disadvantages are that it retrieves more info than just the file size, not for free, and that it has to squeeze any failure into an errno code. – Hans Passant Nov 01 '19 at 15:22
  • 1
    Both `GetFileSize` and `_stat64` are heavily OS-dependent. The former is useless on *ix, and I would have assumed the latter is useless on Windows. If you want to write cross-platform code, this is one situation where you're going to have to use the moral equivalent of an `#ifdef`. If you only care about Windows, I'd go ahead and use `GetFileSize`. ("When in Rome, do as the Romans do.") – Steve Summit Nov 01 '19 at 15:32
  • @SteveSummit actually `_stat64` exists on Windows. – Jabberwocky Nov 01 '19 at 15:37
  • @Jabberwocky Actually, it does not exist on Windows (unless you count msvcrt.dll as a part of the OS). The Microsoft C run-time provides it, not the OS. – Anders Nov 01 '19 at 16:17
  • @Anders yes, that's what I meant, thanks for making it clear. – Jabberwocky Nov 01 '19 at 16:19
  • 3
    `GetFileSize()` (and its successor `GetFileSizeEx()`) takes a file `HANDLE` as input, so it requires you to actually open the file first. `_stat()` takes a file path string instead, and can get the size from the filesystem's metadata without needing to open the file. The Win32 equivalent to that is `FindFirstFile/Ex()` or `GetFileAttributesEx(GetFileExInfoStandard)` (both of which return more than just the file size) – Remy Lebeau Nov 01 '19 at 18:04
  • @Anders, no, accessing the C runtime API is not dependent on msvcrt.dll (which is of course part of the OS, but not the API). The Universal CRT (i.e. ucrtbase.dll and API sets) is a Windows component that is maintained by Windows update in Vista and above. See, for example, [api-ms-win-crt-filesystem-l1-1-0.dll](https://learn.microsoft.com/en-us/uwp/win32-and-com/win32-extension-apis#apis-from-api-ms-win-crt-filesystem-l1-1-0dll). – Eryk Sun Nov 01 '19 at 20:50
  • 2
    @RemyLebeau, the file size is filesystem metadata in all cases. But `FindFirstFile[Ex]W` (i.e. `NtQueryDirectoryFile[Ex]`) queries the parent directory, which may maintain a limited set of separately updated metadata for performance reasons, as NTFS does. This metadata in the directory, especially last-write and and last-access timestamps, is not always in sync with the metadata in the file record itself. – Eryk Sun Nov 01 '19 at 20:55
  • @HansPassant, the presence of `stat` in the C runtime has nothing at all to do with the old POSIX subsystem. The low I/O CRT functions predate Windows NT by many years. MS C has a history dating back to the early 80s. – Eryk Sun Nov 01 '19 at 20:57
  • @RemyLebeau, also, to clarify, in most cases the CRT's `[f]stat` nowadays is based on `CreateFileW` (`NtCreateFile`) and `GetFileInformationByHandle` (`NtQueryVolumeInformationFile` : `FileFsVolumeInformation`, `NtQueryInformatonFile` : `FileAllInformation`) instead of `FindFirstFileW`. Also, not relevant yet, but Windows 10 has a new, streamlined `NtQueryInformationFile` : [`FileStatInformation`](https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_stat_information) query that also includes the caller's granted access; maybe the CRT will use this instead one day. – Eryk Sun Nov 01 '19 at 21:22

0 Answers0