0

I use windows.h library to know how many clusters the file is occupying, how many sectors per cluster there are, and additional information about disk. I need to know the numbers of clusters, not only the count. Is there any way to do this?

Thanks for any help.

This is the code

#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;
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308

0 Answers0