How to find free disk space using C

R

royzlife

Hi,

I am trying to incorporate a code snippet which would calculate the
free disk space of any drive passed to it as argument but presently
stuck with it.
Can anyone help me out with sample code?

I have already tried with the follwoing:
typedef BOOL (WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER,
PULARGE_INTEGER, PULARGE_INTEGER);

void main (int argc, char **argv)
{
BOOL fResult;

char *pszDrive = NULL,
szDrive[4];

DWORD dwSectPerClust,
dwBytesPerSect,
dwFreeClusters,
dwTotalClusters;

P_GDFSE pGetDiskFreeSpaceEx = NULL;

unsigned __int64 i64FreeBytesToCaller,
i64TotalBytes,
i64FreeBytes;

/*
Command line parsing.

If the drive is a drive letter and not a UNC path, append a
trailing backslash to the drive letter and colon. This is
required on Windows 95 and 98.
*/
if (argc != 2)
{
printf ("usage: %s <drive|UNC path>\n", argv[0]);
printf ("\texample: %s C:\\\n", argv[0]);
return;
}

pszDrive = argv[1];

if (pszDrive[1] == ':')
{
szDrive[0] = pszDrive[0];
szDrive[1] = ':';
szDrive[2] = '\\';
szDrive[3] = '\0';

pszDrive = szDrive;
}

/*
Use GetDiskFreeSpaceEx if available; otherwise, use
GetDiskFreeSpace.

Note: Since GetDiskFreeSpaceEx is not in Windows 95 Retail,
we
dynamically link to it and only call it if it is present.
We
don't need to call LoadLibrary on KERNEL32.DLL because it is
already loaded into every Win32 process's address space.
*/
pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress ( ****************
GetModuleHandle ("kernel32.dll"),

"GetDiskFreeSpaceExA");
if (pGetDiskFreeSpaceEx)
{
fResult = pGetDiskFreeSpaceEx (pszDrive, **************

(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);
if (fResult)
{
printf ("\n\nGetDiskFreeSpaceEx reports\n\n");
printf ("Available space to caller = %I64u MB\n",
i64FreeBytesToCaller / (1024*1024));
printf ("Total space = %I64u MB\n",
i64TotalBytes / (1024*1024));
printf ("Free space on drive = %I64u MB\n",
i64FreeBytes / (1024*1024));
}
}
else
{
fResult = GetDiskFreeSpace (pszDrive, **********
&dwSectPerClust,
&dwBytesPerSect,
&dwFreeClusters,
&dwTotalClusters);
if (fResult)
{
/* force 64-bit math */
i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust
*
dwBytesPerSect;
i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust *
dwBytesPerSect;

printf ("GetDiskFreeSpace reports\n\n");
printf ("Free space = %I64u MB\n",
i64FreeBytes / (1024*1024));
printf ("Total space = %I64u MB\n",
i64TotalBytes / (1024*1024));
}
}

if (!fResult)
printf ("error: %lu: could not get free space for \"%s\"\n",
GetLastError(), argv[1]);
}


But this code works fine when i run it separately but when incorporate
the same in my code, its giving 3 warnings:
'function' : incompatible types - from 'char *' to 'const unsigned
short *' at the locations where i have given '*' marks
and the output comes from the "else" block and free space is shown as
0.

Please help me out!!!
 
N

Nick Keighley

I am trying to incorporate a code snippet which would calculate the
free disk space of any drive passed to it as argument

you can't do this in portable C. It requires platform specific code.

but presently stuck with it.
Can anyone help me out with sample code?

I have already tried with the follwoing:
typedef BOOL (WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER,
PULARGE_INTEGER, PULARGE_INTEGER);

ok, that looks like Windows code. Go and ask your question on a
Windows
news group.

void main (int argc, char **argv)

no. It should be

int main (int argc, char **argv)

pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress ( ****************
GetModuleHandle ("kernel32.dll"),

"GetDiskFreeSpaceExA");

But this code works fine when i run it separately but when incorporate
the same in my code, its giving 3 warnings:
'function' : incompatible types - from 'char *' to 'const unsigned
short *' at the locations where i have given '*' marks

work out which argument is wrong (I would have expected the compiler
to
tell you). Check the documentation and fix it.
and the output comes from the "else" block and free space is shown as
0.

you can run a program that doesn't compile...
 
R

royzlife

Hi Nick,
I want code that would find out the disk space on Windows
platform....can you help me out regarding wat else needs to be done?
the code that i pasted was working fine on my machine as individual
project but failing when incorporated in some other code.

I suspect that kernel32.dll was not being loaded prropoerly..but i
need some or the other to do the above!
 
J

jacob navia

Hi,

I am trying to incorporate a code snippet which would calculate the
free disk space of any drive passed to it as argument but presently
stuck with it.
Can anyone help me out with sample code?

[snip code]
But this code works fine when i run it separately but when incorporate
the same in my code, its giving 3 warnings:
'function' : incompatible types - from 'char *' to 'const unsigned
short *' at the locations where i have given '*' marks
and the output comes from the "else" block and free space is shown as
0.

Please help me out!!!


The problem is that within your application UNICODE is defined, i.e. the
character strings are UNICODE character strings, and NOT plain chars.

Either change your application or change the sample code to use UNICODE
chars.

jacob
 
M

Mark McIntyre

On Mon, 18 Jun 2007 01:45:29 -0700, in comp.lang.c ,
Hi Nick,
I want code that would find out the disk space on Windows
platform....can you help me out regarding wat else needs to be done?

Nick already answered your question:
I suspect that kernel32.dll was not being loaded prropoerly..

Exceptionally unlikley, since its part of the OS kernel, but this is
why you absolutely should ask in a Windows group where they know about
such stuff.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

jacob navia

Hi Nick,
I want code that would find out the disk space on Windows
platform....can you help me out regarding wat else needs to be done?
the code that i pasted was working fine on my machine as individual
project but failing when incorporated in some other code.

I suspect that kernel32.dll was not being loaded prropoerly..but i
need some or the other to do the above!

See the reply of jacob navia in this same thread
 
C

CBFalconer

I am trying to incorporate a code snippet which would calculate the
free disk space of any drive passed to it as argument but presently
stuck with it. Can anyone help me out with sample code?

I have already tried with the follwoing:
typedef BOOL (WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER,
PULARGE_INTEGER, PULARGE_INTEGER);
.... snip ...

But this code works fine when i run it separately but when
incorporate the same in my code, its giving 3 warnings:
'function' : incompatible types - from 'char *' to 'const unsigned
short *' at the locations where i have given '*' marks
and the output comes from the "else" block and free space is shown
as 0.

How? That is not standard C coding, so we have no idea what it
does. The only defined word in your header above is "typedef".
Try a newsgroup that deals with your non-standard system (at a
guess, Windows).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top