VC++实现获取文件占用空间大小的两种方法(非文件大小)

时间:2023-03-08 15:53:11
 // GetFileSpaceSize.cpp : Defines the entry point for the console application.
//
/************************************************************************
* author: HwangBae
* created: 2012/07/21
* Blog: http://hwangbae.cnblogs.com/
* Email: hwangbae@live.cn
************************************************************************/ #include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <math.h> int _tmain(int argc, _TCHAR* argv[])
{
if (argc < )
{
_tprintf_s(_T("Usage: GetFileSpaceSize filename\n"));
return -;
} // 文件路径
LPCTSTR szFileName = argv[]; // 打开文件句柄
HANDLE hFile = ::CreateFile(szFileName, GENERIC_READ | FILE_SHARE_READ, ,
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 -;
} // 获取文件大小
UINT64 uFileSize = ;
::GetFileSizeEx(hFile, reinterpret_cast<PLARGE_INTEGER>(&uFileSize));
::CloseHandle(hFile); // 获取磁盘根路径
TCHAR szVolumePathName[] = _T("C:\\");
::GetVolumePathName(szFileName, szVolumePathName, sizeof(szVolumePathName) / sizeof(TCHAR)); // 保存簇信息的变量
DWORD dwSectorsPerCluster = ;
DWORD dwBytesPerSector = ;
DWORD dwNumberOfFreeClusters = ;
DWORD dwTotalNumberOfClusters = ; // 获取簇信息
if (!::GetDiskFreeSpace(
    szVolumePathName, //磁盘根路径
    &dwSectorsPerCluster, //每簇的扇区数
    &dwBytesPerSector, //每扇区的字节数
    &dwNumberOfFreeClusters, //空余簇的数量
    &dwTotalNumberOfClusters //全部簇的数量
    )
)
{
_tprintf_s(_T("Failed to get disk cluster info! error code: %d\n"), GetLastError());
return -;
}
// 簇大小 = 每簇的扇区数 * 每扇区的字节数
DWORD dwClusterSize = dwSectorsPerCluster * dwBytesPerSector; // 计算文件占用空间
// 公式:(以字节为单位)
// 簇数 = 向上取整(文件大小 / 簇大小)
// 占用空间 = 簇数 * 簇大小
UINT64 dwFileSpacesize = static_cast<UINT64>(ceil(uFileSize / static_cast<double>(dwClusterSize)) * dwClusterSize); _tprintf_s(_T("FileName : %s\n"), szFileName);
_tprintf_s(_T("FileSize : %I64u Byte\n"), uFileSize);
_tprintf_s(_T("FileSpacesSize : %I64u Byte\n"), dwFileSpacesize);
return ;
}

方法二:

 // GetFileSpaceSize.cpp : Defines the entry point for the console application.
//
/************************************************************************
* author: HwangBae
* created: 2012/07/23
* Blog: http://hwangbae.cnblogs.com/
* Email: hwangbae@live.cn
************************************************************************/ #include <windows.h>
#include <tchar.h>
#include <stdio.h> #define CLOSE_HANDLE(handle) \
do \
{ \
CloseHandle(handle); \
handle = NULL; \
} while (FALSE) int _tmain(int argc, _TCHAR* argv[])
{
if (argc < )
{
_tprintf_s(_T("Usage: GetFileSpaceSize filename\n"));
return -;
} // 文件路径
LPCTSTR szFileName = argv[]; // 打开文件句柄
HANDLE hFile = ::CreateFile(szFileName, GENERIC_READ | FILE_SHARE_READ, ,
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 -;
} // 获取文件大小
UINT64 uFileSize = ;
::GetFileSizeEx(hFile, reinterpret_cast<PLARGE_INTEGER>(&uFileSize)); FILE_STANDARD_INFO fsi = {};
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 -;
} _tprintf_s(_T("FileName : %s\n"), szFileName);
_tprintf_s(_T("FileSize : %I64u Byte\n"), uFileSize);
_tprintf_s(_T("FileSpacesSize : %I64u Byte\n"), fsi.AllocationSize);
CLOSE_HANDLE(hFile);
return ;
}

转载:http://www.cnblogs.com/hwangbae/archive/2012/07/21/2602592.html