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
long GetFileSize(const TCHAR *fileName)
{
BOOL fOk;
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
if (NULL == fileName)
return -1;
fOk = GetFileAttributesEx(fileName, GetFileExInfoStandard, (void*)&fileInfo);
if (!fOk)
return -1;
assert(0 == fileInfo.nFileSizeHigh);
return (long)fileInfo.nFileSizeLow;
}
|