简体   繁体   English

从 GB 转换为 DWORD 或反之亦然

[英]Conversion from GB to DWORD or vice-versa

I want to compare a filesize to check if it is below 8GB.我想比较文件大小以检查它是否低于 8GB。 How could I convert either the filseSize( which is in DWORD) to GB or the 8 GB to DWORD?如何将 filseSize(以 DWORD 格式)转换为 GB 或将 8 GB 转换为 DWORD?

Thank you!谢谢!

I'm guessing from your DWORD hint, you are on the Windows platform and using Win32 APIs to get file sizes.我从您的DWORD提示中猜测,您在 Windows 平台上并使用 Win32 API 获取文件大小。

Don't call GetFileSize .不要调用GetFileSize It's limited to a DWORD (32-bit), which I think it what your question is about.它仅限于 DWORD(32 位),我认为这是您的问题所在。

Instead, invoke GetFileSizeEx , which gives you back a 64-bit result for a file handle.相反,请调用GetFileSizeEx ,它会为您返回文件句柄的 64 位结果。 Or GetFileAttributesEx which gives back a struct with a 64-bit size in it split across two dwords.或者GetFileAttributesEx ,它返回一个 64 位大小的结构,分成两个双字。

Example:例子:

const LONGLONG MAX_FILE_SIZE = 1024LL * 1024LL * 1024LL * 8;
LARGE_INTEGER li = {0};
if (GetFileSizeEx(hFile, &li)) {
    LONGLONG filesize = li.QuadPart;
    if (filesize > MAX_FILE_SIZE) {
        ...
    }
}

OR或者

const LONGLONG MAX_FILE_SIZE = 1024LL * 1024LL * 1024LL * 8;
WIN32_FILE_ATTRIBUTE_DATA info = {0};
if (GetFileAttributesEx(filename, GetFileExInfoStandard, (void*)&info)) {
    LONGLONG filesize = info.nFileSizeHigh;
    filesize  = filesize << 32;
    filesize |= info.nFileSizeLow;
    if (filesize > MAX_FILE_SIZE) {
        ...
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM