QT C++ 获取文件占用空间 簇

获取文件大小跟实际占用大小不同 药计算簇的大小才能实际拿到文件在内存的真实占用空间

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#define CLOSE_HANDLE(handle) \
   do \
   { \
       CloseHandle(handle); \
       handle = NULL; \
   } while (FALSE)



int function()
{

 const wchar_t * str_cpu = reinterpret_cast<const wchar_t *>(fileInfo.absoluteFilePath().utf16());
            LPCTSTR szFileName = str_cpu;//reinterpret_cast<LPCWSTR>(testStringArr.data());//reinterpret_cast<LPCWSTR>(fileInfo.absoluteFilePath().toLocal8Bit().toStdString().c_str());
            // 打开文件句柄
                 HANDLE hFile = ::CreateFile(szFileName, GENERIC_READ | FILE_SHARE_READ, 0,
                     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                 if (hFile == INVALID_HANDLE_VALUE)
                 {
                     _tprintf_s(_T("Failed to create file handle: %s ! error code:%d\n"), szFileName, GetLastError());
                     return fileList;
                 }

                 // 获取文件大小
                 UINT64 uFileSize = 0;
                 ::GetFileSizeEx(hFile, reinterpret_cast<PLARGE_INTEGER>(&uFileSize));

                 FILE_STANDARD_INFO fsi = {0};
                 if (!::GetFileInformationByHandleEx(hFile, FileStandardInfo, &fsi, sizeof(FILE_STANDARD_INFO)))
                 {
                     _tprintf_s(_T("Failed to get file info! error code:%d\n"), GetLastError());
                     CLOSE_HANDLE(hFile);
                     return fileList;
                 }

                // _tprintf_s(_T("FileName : %s\n"), szFileName);
            //     qDebug() << uFileSize ;
                // printf(_T("FileSpacesSize : %I64u Byte\n",fsi.AllocationSize)
               //  _tprintf_s(_T("FileSize : %I64u Byte\n"), uFileSize);
                // ULONGLONG QuadPart = fsi.AllocationSize;
                 //printf_(_T("FileSpacesSize : %I64u Byte\n"), fsi.AllocationSize);
              //   qDebug() << fsi.AllocationSize.QuadPart;
               //  _tprintf_s(_T("FileSpacesSize : %I64u Byte\n"), fsi.AllocationSize);
                 CLOSE_HANDLE(hFile);
}


版权声明:本文为qq_42095701原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。