Finding the correct files larger than 4 GB in windows

I used this C ++ code to find the file size of some files in windows (using visual studio):

(p_findFileData->nFileSizeHigh * MAXDWORD) + p_findFileData->nFileSizeLow);

This did not give me the correct file if the file was larger than 4 GB. After some research, I tried:

(p_findFileData->nFileSizeHigh * (MAXDWORD+1)) + p_findFileData->nFileSizeLow); 

since I read that nfilesizehigh and nfilesizelow are 32 bits of each of the 64-bit value of the sizeize file, and if the file size value is more than 32 bits, we multiply maxdword (which in my case is 0xffffffff) by nfilesizehigh. The second solution also did not work and gave me a smaller size than it was. I tried this again:

ULONGLONG FileSize = (FindFileData.nFileSizeHigh * 4294967296) + FindFileData.nFileSizeLow;

and it worked. I also used another solution to get the file size along with this:

 ULONGLONG FileSize = FindFileData.nFileSizeHigh;
 FileSize <<= sizeof( FindFileData.nFileSizeHigh ) *8; 
 FileSize |= FindFileData.nFileSizeLow;

The above solution also works:

, 2 , , , . .

+5
3

:

ULONGLONG FileSize = (static_cast<ULONGLONG>(FindFileData.nFileSizeHigh) <<
                      sizeof(FindFileData.nFileSizeLow) *8) |
                     FindFileData.nFileSizeLow;

ULONGLONG , 32- 64- , FileSize.

, , "" - , , [, , .

"" :

:

 (p_findFileData->nFileSizeHigh * MAXDWORD) + p_findFileData->nFileSizeLow);

, MAXDWORD 4 , [, , , 1 , , ]. , 32- , :

 -p_findFileData->nFileSizeHigh + p_findFileData->nFileSizeLow;

MAXDWORD -1 (, , , , ).

, - , 32- .

 (p_findFileData->nFileSizeHigh * (MAXDWORD+1)) + p_findFileData->nFileSizeLow);

, low part + (0 * high part), , , .

:

 static_cast<ULONGLONG>(p_findFileData->nFileSizeHigh) * (MAXDWORD+1) +
 p_findFileData->nFileSizeLow;
+8

ULARGE_INTEGER , / :

ULARGE_INTEGER ul;
ul.HighPart = p_findFileData->nFileSizeHigh;
ul.LowPart = p_findFileData->nFileSizeLow;
ULONGLONG FileSize = ul.QuadPart;
+7
(p_findFileData->nFileSizeHigh * MAXDWORD) + p_findFileData->nFileSizeLow);

This does not give the correct result, because MAXDWORD is the wrong value.

(p_findFileData->nFileSizeHigh * (MAXDWORD+1)) + p_findFileData->nFileSizeLow); 

This does not work because you add +1 to MAXDWORD before giving this type a value large enough to hold the value.

(FindFileData.nFileSizeHigh * 4294967296) + FindFileData.nFileSizeLow;

This works because it 4294967296is a 64-bit type.

FileSize <<= sizeof( FindFileData.nFileSizeHigh ) *8; 
FileSize |= FindFileData.nFileSizeLow;

This works because you offset the most significant bits by 32 (which is identical to multiplying by 4294967296), and then β€œadds” the least significant bits using bitwise or.

+3
source

All Articles