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);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值