Using _getdiskfree()

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Getting the drives physical information

To show: Using _getdiskfree()

 

// Getting info on the available drives

#include <windows.h>

#include <direct.h>

#include <stdio.h>

#include <tchar.h>

 

TCHAR g_szText[] = _T("Drive Total_clus Available_clus Sec/Cluster Bytes/Sec\n");

TCHAR g_szText1[] = _T("----- ---------- -------------- ----------- ---------\n");

TCHAR g_szInfo[] = _T("-> \n");

 

// For data display format. Right justified, thousand comma separated and other format for displayed data

void utoiRightJustified(TCHAR* szLeft, TCHAR* szRight, unsigned uValue)

{

TCHAR* szCur = szRight;

int nComma = 0;

if(uValue)

{

while(uValue && (szCur >= szLeft))

{

if(nComma == 3)

{

*szCur = ',';

nComma = 0;

}

else

{

*szCur = (uValue % 10) | 0x30;

uValue /= 10;

++nComma;

}

--szCur;

}

}

else

{

*szCur = '0';

--szCur;

}

if(uValue)

{

szCur = szLeft;

while(szCur <= szRight)

{// If not enough field to display the data...

*szCur = '*';

++szCur;

}

}

}

 

int main(void)

{

TCHAR szMsg[4200];

struct _diskfree_t df = {0};

// Search drives and assigns the bit masks to uDriveMask variable

ULONG uDriveMask = _getdrives();

unsigned uErr, uLen, uDrive;

printf("clus - cluster, sec - sector\n");

 

// %S - String: When used with printf functions, specifies a wide-character string;

// when used with wprintf functions, specifies a single-byte character string.

// Characters are printed up to the first null character or until the precision value is reached.

printf("%S", g_szText);

printf("%S", g_szText1);

for(uDrive = 1; uDrive <26>>= 1;

}

return 0;

}

 

Output example:

 

clus - cluster, sec - sector

Drive Total_clus Available_clus Sec/Cluster Bytes/Sec

----- ---------- -------------- ----------- ---------

-> A 2,847 2,436 1 512

-> C 2,560,351 171,673 8 512

-> E 2,560,351 2,352,868 8 512

-> F 2,560,351 2,111,267 8 512

-> G 2,353,514 1,875,680 8 512

-> J 63,419 5,717 16 512

-> K 225,101 0 1 2,048

Press any key to continue . . .

 

 

Note: A: is floppy, J: is USB's thumb drive and K: is CDRW. A floppy and CD are inserted when the program is run.

 

 

C and C++ Programming Resources | C & C++ Code Example Index