I tried to get file size for 16 GB txt file. but I got different size with real size. who can help me?
HANDLE FileHandle = INVALID_HANDLE_VALUE;
long long FileSize;
FileHandle = CreateFileA(szInputFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
if(FileHandle == INVALID_HANDLE_VALUE)
return;
FileSize = GetFileSize(FileHandle, NULL);
CodePudding user response:
As you can see in the GetFileSize
documentation:
If the file size exceeds 32bit, you must provide the 2nd argument, to be filled with the 32 high bits of the size.
Then you can combine the 2 in order to get the final file size.
Code example:
HANDLE FileHandle = INVALID_HANDLE_VALUE;
// ... (initialize FileHandle)
DWORD FileSizeLow;
DWORD FileSizeHigh;
//------------------------------------vvvvvvvvvvvvvv-
FileSizeLow = GetFileSize(FileHandle, &FileSizeHigh);
uint64_t FileSize = ((uint64_t)FileSizeHigh << 32) FileSizeLow;
// ...
Alternatively, as @IInspectable commented, you can use GetFileSizeEx
:
LARGE_INTEGER FileSizeL;
if (GetFileSizeEx(FileHandle, &FileSizeL))
{
LONGLONG FileSize = FileSizeL.QuadPart; // 64bit signed
// ...
}
else
{
// Handle error ...
}
CodePudding user response:
The GetFileSize
documentation recommends using GetFileSizeEx
instead which is simpler:
LARGE_INTEGER FileSize;
if (!GetFileSizeEx(FileHandle, &FileSize))
{
std::cout << "error getting file size\n";
}
else
{
std::cout << "file size: " << FileSize.QuadPart << "\n";
}
for a more c solution std::filesystem::file_size
(though this only works with a path, not a handle):
auto FileSize = std::filesystem::file_size(szInputFileName)
CodePudding user response:
With using this code, I could get over 10GB of file size successfully.
unsigned long long GetFileSize1(char* path)
{
std::ifstream fstrm(path, ios_base::in | ios_base::binary);
fstrm.seekg(0, ios_base::end);
long long filesize = fstrm.tellg();
return filesize;
}