赞助

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

 1 // GetFileSpaceSize.cpp : Defines the entry point for the console application.
 2 //
 3 /************************************************************************
 4  * author: HwangBae
 5  * created:    2012/07/21
 6  * Blog: http://hwangbae.cnblogs.com/
 7  * Email: hwangbae@live.cn
 8  ************************************************************************/
 9 
10 #include <windows.h>
11 #include <tchar.h>
12 #include <stdio.h>
13 #include <math.h>
14 
15 
16 int _tmain(int argc, _TCHAR* argv[])
17 {
18     if (argc < 2)
19     {
20         _tprintf_s(_T("Usage: GetFileSpaceSize filename\n"));
21         return -1;
22     }
23 
24     // 文件路径
25     LPCTSTR szFileName = argv[1];
26 
27     // 打开文件句柄
28     HANDLE hFile = ::CreateFile(szFileName, GENERIC_READ | FILE_SHARE_READ, 0, 
29         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
30     if (hFile == INVALID_HANDLE_VALUE)
31     {
32         _tprintf_s(_T("Failed to create file handle: %s ! error code:%d\n"), szFileName, GetLastError());
33         return -1;
34     }
35 
36     // 获取文件大小
37     UINT64 uFileSize = 0;
38     ::GetFileSizeEx(hFile, reinterpret_cast<PLARGE_INTEGER>(&uFileSize));
39     ::CloseHandle(hFile);
40 
41     // 获取磁盘根路径
42     TCHAR szVolumePathName[] = _T("C:\\");
43     ::GetVolumePathName(szFileName, szVolumePathName, sizeof(szVolumePathName) / sizeof(TCHAR));
44 
45     // 保存簇信息的变量
46     DWORD dwSectorsPerCluster = 0;
47     DWORD dwBytesPerSector = 0;
48     DWORD dwNumberOfFreeClusters = 0;
49     DWORD dwTotalNumberOfClusters = 0;
50 
51     // 获取簇信息
52     if (!::GetDiskFreeSpace(
53             szVolumePathName,            //磁盘根路径
54             &dwSectorsPerCluster,        //每簇的扇区数
55             &dwBytesPerSector,            //每扇区的字节数
56             &dwNumberOfFreeClusters,    //空余簇的数量
57             &dwTotalNumberOfClusters    //全部簇的数量
58             )
59         )
60     {
61         _tprintf_s(_T("Failed to get disk cluster info! error code: %d\n"), GetLastError());
62         return -1;
63     }
64     // 簇大小 = 每簇的扇区数 * 每扇区的字节数
65     DWORD dwClusterSize = dwSectorsPerCluster * dwBytesPerSector;
66 
67     // 计算文件占用空间
68     // 公式:(以字节为单位)
69     // 簇数 = 向上取整(文件大小 / 簇大小)
70     // 占用空间 = 簇数 * 簇大小
71     UINT64 dwFileSpacesize = static_cast<UINT64>(ceil(uFileSize / static_cast<double>(dwClusterSize)) * dwClusterSize);
72 
73     _tprintf_s(_T("FileName : %s\n"), szFileName);
74     _tprintf_s(_T("FileSize : %I64u Byte\n"), uFileSize);
75     _tprintf_s(_T("FileSpacesSize : %I64u Byte\n"), dwFileSpacesize);
76     return 0;
77 }

 

 

方法二:

 1 // GetFileSpaceSize.cpp : Defines the entry point for the console application.
 2  //
 3  /************************************************************************
 4   * author: HwangBae
 5   * created:    2012/07/23
 6   * Blog: http://hwangbae.cnblogs.com/
 7   * Email: hwangbae@live.cn
 8   ************************************************************************/
 9  
10  #include <windows.h>
11  #include <tchar.h>
12  #include <stdio.h>
13  
14  #define CLOSE_HANDLE(handle) \
15      do \
16      { \
17          CloseHandle(handle); \
18          handle = NULL; \
19      } while (FALSE)
20  
21  int _tmain(int argc, _TCHAR* argv[])
22  {
23      if (argc < 2)
24      {
25          _tprintf_s(_T("Usage: GetFileSpaceSize filename\n"));
26          return -1;
27      }
28  
29      // 文件路径
30      LPCTSTR szFileName = argv[1];
31  
32      // 打开文件句柄
33      HANDLE hFile = ::CreateFile(szFileName, GENERIC_READ | FILE_SHARE_READ, 0, 
34          NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
35      if (hFile == INVALID_HANDLE_VALUE)
36      {
37          _tprintf_s(_T("Failed to create file handle: %s ! error code:%d\n"), szFileName, GetLastError());
38          return -1;
39      }
40  
41      // 获取文件大小
42      UINT64 uFileSize = 0;
43      ::GetFileSizeEx(hFile, reinterpret_cast<PLARGE_INTEGER>(&uFileSize));
44  
45      FILE_STANDARD_INFO fsi = {0};
46      if (!::GetFileInformationByHandleEx(hFile, FileStandardInfo, &fsi, sizeof(FILE_STANDARD_INFO)))
47      {
48          _tprintf_s(_T("Failed to get file info! error code:%d\n"), GetLastError());
49          CLOSE_HANDLE(hFile);
50          return -1;
51      }
52  
53      _tprintf_s(_T("FileName : %s\n"), szFileName);
54      _tprintf_s(_T("FileSize : %I64u Byte\n"), uFileSize);
55      _tprintf_s(_T("FileSpacesSize : %I64u Byte\n"), fsi.AllocationSize);
56      CLOSE_HANDLE(hFile);
57      return 0;
58  }

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

posted @ 2016-01-18 11:29  车臣  阅读(996)  评论(0编辑  收藏  举报