Home / Get file size under windows edit
Try Documentalist, my app that offers fast, offline access to 190+ programmer API docs.

Windows doesn’t have an API to get a file size based on file name. This small function does that.
It returns -1 if a file doesn’t exist.
It doesn’t handle files > 2 GB (max positive number for 32 bit signed value). It’s quite easy to extend it to 64-bits if you know what is the 64 bit integer type in your compiler (unfortunately there’s no standard).
A better design might be BOOL GetFileSize(const TCHAR *fileName, unsigned long *fileSizeOut) i.e. returning false if file doesn’t exist and putting the file size into fileSizeOut.
long GetFileSize(const TCHAR *fileName)
    BOOL                        fOk;

    if (NULL == fileName)
        return -1;

    fOk = GetFileAttributesEx(fileName, GetFileExInfoStandard, (void*)&fileInfo);
    if (!fOk)
        return -1;
    assert(0 == fileInfo.nFileSizeHigh);
    return (long)fileInfo.nFileSizeLow;

Feedback about page:

Optional: your email if you want me to get back to you:

Need fast, offline access to 190+ programmer API docs? Try my app Documentalist for Windows