Function
GetFileSize determines the size of a specified file.
Syntax
DWORD GetFileSize(
[in] HANDLE hFile,
[out] LPDWORD lpFileSizeHigh
);
Parameters
| Parameter | Type | Description |
|---|---|---|
hFile |
HANDLE |
A handle to the file whose size is to be retrieved. The handle must have been created with the GENERIC_READ access right. |
lpFileSizeHigh |
LPDWORD |
A pointer to a DWORD variable that receives the high-order 32 bits of the file size. This parameter can be NULL if the file size fits within the low-order 32 bits. |
Return Value
If the function succeeds, the return value is the low-order 32 bits of the file size. If the file size is greater than 4 GB, the return value is INVALID_FILE_SIZE and the high-order bits are returned in the buffer pointed to by lpFileSizeHigh.
If the function fails, the return value is INVALID_FILE_SIZE and GetLastError is set to an appropriate error code.
Remarks
To get the size of a file, you typically use the GetFileSizeEx function. The GetFileSize function is provided for compatibility with earlier versions of Windows.
The lpFileSizeHigh parameter is used to retrieve file sizes larger than 4 GB. If lpFileSizeHigh is not NULL, the high-order 32 bits of the size are returned in the DWORD value pointed to by lpFileSizeHigh. The low-order 32 bits are returned by the function.
If the file size is less than 4 GB, the value returned in lpFileSizeHigh is usually 0.
The INVALID_FILE_SIZE return value is a 32-bit value, which is 0xFFFFFFFF. Because file sizes are unsigned, a return value of INVALID_FILE_SIZE is not a possible file size.
Example
#include <windows.h>
#include <iostream>
int main() {
HANDLE hFile = CreateFile(
L"example.txt", // File name
GENERIC_READ, // Access mode
FILE_SHARE_READ, // Share mode
NULL, // Security attributes
OPEN_EXISTING, // Creation disposition
FILE_ATTRIBUTE_NORMAL, // Flags and attributes
NULL // Template file
);
if (hFile == INVALID_HANDLE_VALUE) {
std::cerr << "Error creating file handle: " << GetLastError() << std::endl;
return 1;
}
DWORD fileSizeLow = GetFileSize(hFile, NULL);
if (fileSizeLow == INVALID_FILE_SIZE) {
std::cerr << "Error getting file size: " << GetLastError() << std::endl;
CloseHandle(hFile);
return 1;
}
std::cout << "File size (low 32 bits): " << fileSizeLow << " bytes" << std::endl;
DWORD fileSizeHigh = 0;
fileSizeLow = GetFileSize(hFile, &fileSizeHigh);
if (fileSizeLow == INVALID_FILE_SIZE) {
std::cerr << "Error getting file size (high bits): " << GetLastError() << std::endl;
CloseHandle(hFile);
return 1;
}
if (fileSizeHigh > 0) {
std::cout << "File size (high 32 bits): " << fileSizeHigh << " bytes" << std::endl;
std::cout << "Total file size: " << (static_cast<ULONGLONG>(fileSizeHigh) << 32) + fileSizeLow << " bytes" << std::endl;
}
CloseHandle(hFile);
return 0;
}
Requirements
| Minimum supported client | Windows 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Header | fileapi.h (include windows.h) |
| Library | Kernel32.lib |
| DLL | Kernel32.dll |