Thread: Information about disk

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question Information about disk

    Hi!

    I'm having troubles with free disk code. Please take a look at my code.

    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <windows.h>
    int main ()
    {
        DWORD dwSectorsPerCluster, dwBytesPerSector, dwNumberOfFreeClusters, dwTotalNumberOfClusters, freeSpace;
    
        if (GetDiskFreeSpace (NULL, &dwSectorsPerCluster, &dwBytesPerSector, &dwNumberOfFreeClusters, &dwTotalNumberOfClusters))
        {
            freeSpace = dwSectorsPerCluster * dwBytesPerSector * dwTotalNumberOfClusters;
            gotoxy (10, 21);
            printf ("Free space: %u", space/1024/1024/1024);
        }
        getch ();
        return 0;
    }
    This code does not work and I don't know why. Please help me.

    How to retrieve a disk capacity information?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Just how many /1024 do you need?

    What do you want the answer in - KB, MB or what?

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Three. I need the answer in GB.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    # include <stdio.h>
    # include <windows.h>
    int main ()
    {
        DWORD dwSectorsPerCluster,
    		  dwBytesPerSector,
    		  dwNumberOfFreeClusters,
    		  dwTotalNumberOfClusters;
    	double dSpace;	 
    
        if (GetDiskFreeSpace (NULL, &dwSectorsPerCluster,
    			&dwBytesPerSector, &dwNumberOfFreeClusters,
    			&dwTotalNumberOfClusters))
        {
            dSpace = (double)dwBytesPerSector;
    		dSpace *= dwSectorsPerCluster;
    		dSpace *= dwNumberOfFreeClusters;
    		
            dSpace /= 1024;
    		dSpace /= 1024;
    		dSpace /= 1024;
            printf ("Free space: %.2f GB\n", dSpace);
        }
    
        return 0;
    }

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Thanks. But how can I get information about disk capacity?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by GaPe
    Thanks. But how can I get information about disk capacity?
    Gee....just look at the function....

    Code:
    # include <stdio.h>
    # include <windows.h>
    int main ()
    {
        DWORD dwSectorsPerCluster,
    		  dwBytesPerSector,
    		  dwNumberOfFreeClusters,
    		  dwTotalNumberOfClusters;
    	double dSpace;	 
    
        if (GetDiskFreeSpace (NULL, &dwSectorsPerCluster,
    			&dwBytesPerSector, &dwNumberOfFreeClusters,
    			&dwTotalNumberOfClusters))
        {
            dSpace = (double)dwBytesPerSector;
    		dSpace *= dwSectorsPerCluster;
    		dSpace *= dwTotalNumberOfClusters;
    		
            dSpace /= 1024;
    		dSpace /= 1024;
    		dSpace /= 1024;
            printf ("Total space: %.2f GB\n", dSpace);
        }
    
        return 0;
    }

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I don't get it. I get result 0.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by GaPe
    I don't get it. I get result 0.
    Hmm...works for me.....how big is your Drive in reality

  9. #9
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    20 GB is the capacity. Now I fixed the code and I get the result 3767234. Is this right? How to convert it into a GB form?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by GaPe
    20 GB is the capacity. Now I fixed the code and I get the result 3767234. Is this right? How to convert it into a GB form?
    Please post the code....as you are working with it to get that result

  11. #11
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Code:
    if (GetDiskFreeSpace (NULL, &dwSectorsPerCluster, &dwBytesPerSector, &dwNumberOfFreeClusters, &dwTotalNumberOfClusters))
    {
        freeSpace  = dwSectorsPerCluster * dwBytesPerSector * dwTotalNumberOfClusters;
        printf ("\nFree space: %0.2f GB", freeSpace/1024/1024/1024);
        printf ("\nCapacity: %u", dwTotalNumberOfClusters);
    }
    Is there any other function for retrieving the information about the disk?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    What is freeSpace? Is it a double like in my example...or is it still a DWORD?

    Code:
    printf ("\nCapacity: %u", dwTotalNumberOfClusters);
    This is just telling you the amount of clusters....not bytes!!...

  13. #13
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    It's double. I heard somewhere that one cluster is 512 bytes long. If I multiply 512 with dwTotalNumberOfClusters then I get the right result. I want to be sure, is this right?

    EDIT: Damn, I think is wrong. I get 1928823808.
    Last edited by GaPe; 08-04-2002 at 10:59 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by GaPe
    It's double. I heard somewhere that one cluster is 512 bytes long. If I multiply 512 with dwTotalNumberOfClusters then I get the right result. I want to be sure, is this right?
    Dont use literals for that.....use the result of the API you are calling....it gives you all the info you need!!

    For Example....you call GetDiskFreeSpace with pointers to values that will hold;
    [list=1][*]Total sectors per cluster[*]Total bytes per sector[*]Total Free clusters[*]Total number of clusters[/list=1]

    Therefore, total free unused space (in bytes) on a disk = Total bytes per sector * Total sectors per cluster * Total Free clusters

    And

    Total number of bytes on a disk = Total bytes per sector * Total sectors per cluster * Total number of clusters

    Now once you have those numbers, you divide 3 times by 1024 (one for bytes to KB....one fore KB to MB....once more for MB to GB)....As a DWORD (which is the prefered param to this function) holds an integer, you convert the total number of bytes to a double before doing the division....

    Have a play with it...read through the examples given......that should set you on the right path

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > dwSectorsPerCluster * dwBytesPerSector * dwTotalNumberOfClusters
    print out each of these as well to help with debugging
    Your calculation is correct, if these numbers are correct

    And what about the first parameter being NULL - what does that mean - the current disk, the 'windows' disk or what?

    > It's double.
    Odd, I thought they were dwords - so the format should be %lu, not %f

    > 20 GB is the capacity
    From MSDN
    The GetDiskFreeSpace function cannot report volume sizes that are greater than 2 GB. To ensure that your application works with large capacity hard drives, use the GetDiskFreeSpaceEx function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. hard disk information
    By rehan in forum Windows Programming
    Replies: 12
    Last Post: 08-29-2008, 09:37 PM
  3. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  4. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 08:20 PM
  5. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM