Windows API Reference

GetFileSizeEx Function

Retrieves the size of the specified file.

C++ Syntax Copy

BOOL GetFileSizeEx(
  [in]  HANDLE  hFile,
  [out] PLARGE_INTEGER lpFileSize
);
                    

Parameters

Parameter Description
hFile A handle to the file.
lpFileSize A pointer to a LARGE_INTEGER value that receives the file size, in bytes.

Return Value

Returns TRUE if the function succeeds or FALSE if the function fails. To get extended error information, call GetLastError.

Remarks

This function is a replacement for the GetFileSize function. It can return file sizes up to 2^64 bytes.

To get the size of a file, you must open the file with at least read access. For more information, see CreateFile.

The LARGE_INTEGER structure is a union that represents a 64-bit integer. The QuadPart member holds the 64-bit value.

Example

C++ Example Copy

#include <windows.h>
#include <iostream>

int main() {
    HANDLE hFile = CreateFile(
        L"MyFile.txt",
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );

    if (hFile == INVALID_HANDLE_VALUE) {
        std::cerr << "Error opening file: " << GetLastError() << std::endl;
        return 1;
    }

    LARGE_INTEGER fileSize;
    if (GetFileSizeEx(hFile, &fileSize)) {
        std::cout << "File size: " << fileSize.QuadPart << " bytes." << std::endl;
    } else {
        std::cerr << "Error getting file size: " << GetLastError() << std::endl;
    }

    CloseHandle(hFile);
    return 0;
}
                    
Important: Ensure that the file handle passed to GetFileSizeEx is valid and that the process has the necessary permissions to access the file.

See Also