我使用windows.h库来了解文件占用的集群数,每个集群有多少个扇区,以及有关磁盘的其他信息。 我需要知道簇的数量,而不仅仅是计数。 有没有办法做到这一点?
感谢您的帮助。
这是代码
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
WIN32_FIND_DATA search_data;
DWORD fileSize;
LPCSTR pszDrive = NULL;
BOOL test, fResult;
__int64 lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes;
DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters;
int clsCount;
// If the function succeeds, the return value is nonzero. If the function fails, the return value is 0 (zero).
test = GetDiskFreeSpaceEx(pszDrive,
(PULARGE_INTEGER)&lpFreeBytesAvailable,
(PULARGE_INTEGER)&lpTotalNumberOfBytes,
(PULARGE_INTEGER)&lpTotalNumberOfFreeBytes
);
cout << "\nUsing GetDiskFreeSpaceEx()..." << endl;
cout << "The return value: " << test << " error code: " << GetLastError() << endl;
cout << "Total number of free bytes available for user-caller: " << lpFreeBytesAvailable << endl;
cout << "Total number of bytes available for user: " << lpTotalNumberOfBytes << endl;
cout << "Total number of free bytes on disk: " << lpTotalNumberOfFreeBytes << endl;
fResult = GetDiskFreeSpace(pszDrive, &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters);
cout << "\nUsing GetDiskFreeSpace()..." << endl;
cout << "The return value: " << fResult << " error code: " << GetLastError() << endl;
cout << "Sector per cluster = " << dwSectPerClust << endl;
cout << "Bytes per sector = " << dwBytesPerSect << endl;
cout << "Free cluster = " << dwFreeClusters << endl;
cout << "Total cluster = " << dwTotalClusters << endl;
cout << "Total free bytes = " << (dwFreeClusters*dwSectPerClust*dwBytesPerSect) << endl;
HANDLE handle = FindFirstFile("c:\\*", &search_data);
while(handle != INVALID_HANDLE_VALUE)
{
fileSize = search_data.nFileSizeLow + search_data.nFileSizeHigh*(MAXDWORD + 1);
if (fileSize == 0){
cout << "\n" << search_data.cFileName << "\t" << "Folder";
}
else{
clsCount = (fileSize / (dwSectPerClust*dwBytesPerSect)) + 1;
cout << "\n" << search_data.cFileName << "\t" << "bytes: "<<fileSize << "\t" << "clusters count: "<< clsCount;
}
if(FindNextFile(handle, &search_data) == FALSE)
break;
}
//Close the handle after use or memory/resource leak
FindClose(handle);
cin.get();
cin.ignore();
return 0;
}