Problem with formatsize

I have this function in Builder C++ 6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
 AnsiString temp;
        AnsiString Drive = AnsiString(DriveComboBox1->Drive); //EditDrive->Text;

        unsigned int drivetype = GetDriveType(Drive.c_str ());
switch (drivetype)
    {
        case 1 : temp = "No root directory"; return;
        case DRIVE_REMOVABLE : temp = "Removable"; break;
        case DRIVE_FIXED : temp = "Fixed"; break;
        case DRIVE_REMOTE : temp ="Remote (network) drive"; break;
        case DRIVE_CDROM : temp = "CD-ROM"; break;
        case DRIVE_RAMDISK : temp = "RAM disk"; break;
        default: temp = "Unknown"; return;
     }
        LabelDriveType->Caption = temp;
        temp = "";
        DWORD VolumeSerialNumber = 0;
        DWORD MaximumComponentLength = 0;
        DWORD FileSystemFlags = 0;
        char * volumeinfo = new char[255];
        volumeinfo[0] = 0;
        char* FileSystemNameBuffer = new char[255];
        FileSystemNameBuffer[0] = 0;
        GetVolumeInformation (Drive.c_str (), volumeinfo, 255,&VolumeSerialNumber,
        &MaximumComponentLength, &FileSystemFlags,
        FileSystemNameBuffer,255);
        if (strlen(volumeinfo) != 0)
                EditVolumeInfo->Text = volumeinfo ;
        else
                EditVolumeInfo->Text = "- no label -";
//Translate integer to chars for serial number
        char string1[35];
        char string2[35];
        if (VolumeSerialNumber > 0)
                {
                        unsigned int bottom = (LOWORD(VolumeSerialNumber));
                        unsigned int top = (HIWORD(VolumeSerialNumber));
                        sprintf(string1,"%04X",top);
                        sprintf(string2,"%04X",bottom);
                        LabelSerialNum->Caption = AnsiString(string1) + "-" + AnsiString(string2);
                }
        else
        {
                LabelSerialNum->Caption = "- unknown -";
        if (MaximumComponentLength > 0)
                LabelMaxComponentLength->Caption = AnsiString(MaximumComponentLength) + "characters";
        else
                LabelMaxComponentLength->Caption = "- unknown -";
        if (strlen(FileSystemNameBuffer) != 0)
                LabelFileSystemNameBuffer->Caption = FileSystemNameBuffer;
        else
                LabelFileSystemNameBuffer->Caption = "- unknown -";
                LabelFileSystemFlags->Caption = ""; //AnsiString(FileSystemFlags);
        if (FileSystemFlags & FS_CASE_IS_PRESERVED)
                temp += AnsiString("Filename case is preserved.\n");
        if (FileSystemFlags & FS_CASE_SENSITIVE)
                temp += AnsiString("Lookup is case-sensitive.\n");
        if (FileSystemFlags & FS_UNICODE_STORED_ON_DISK)
                temp += AnsiString("Supports Unicode in filenames.\n");
        if (FileSystemFlags & FS_PERSISTENT_ACLS)
                temp += AnsiString("Preserves and enforces ACLs.\n");
        if (FileSystemFlags & FS_FILE_COMPRESSION)
                temp += AnsiString("Supports file-based compression.\n");
        if (FileSystemFlags & FS_VOL_IS_COMPRESSED)
                temp += AnsiString("Volume is compressed. (i.e., DoubleSpace).\n");
        LabelFileSystemFlags->Caption = temp;
        DWORD spc = 0; //Sectors per cluster
        DWORD bps = 0; //Bytes per cluster
        DWORD cluster = 0; //clusters
        DWORD freeclust = 0; //freeclusters
        GetDiskFreeSpace (Drive.c_str (),&spc,&bps,&freeclust,&cluster) ;
        unsigned long v1 = (unsigned long)cluster;
        unsigned long v2 = (unsigned long) spc;
        unsigned long v3 = (unsigned long) bps;
        unsigned long volsize = (v1 * v2)/1024 * v3;
LabelVolumeSize->Caption = AnsiString(FormatSize(volsize));
        unsigned long free_bytes = (freeclust * spc)/1024 * bps;
LabelFreeSpace->Caption = AnsiString(FormatSize(free_bytes));
        if (volsize > 0)
                LabelUsed->Caption = AnsiString(((volsize - free_bytes) * 100) / volsize) +" %";
        else
                LabelUsed->Caption = "n/a";
}
};


I couldn't compile because this 2 lines:
LabelVolumeSize->Caption = AnsiString(FormatSize(volsize));
LabelFreeSpace->Caption = AnsiString(FormatSize(free_bytes));

because it says:
Call to undefined function Formatsize?
Can anybody help me?
Formatsize is undefined. From wherever you took this code, library to be included or function definition should be there.

edit.: and Formatsize is probably just formatting number variable into string so you can easily write your own version.
Last edited on
I take this code from Builder C++ Developer's Guide, which is a book, and i don't understand why this function is there, and is not defined. I supposed i have to include a library, i mean a ".h" file in order to use this function.
And the solution you gave me, is writting my own version of a function, asumming that this funtion converts an integer into string. Ok i will proove.
Thanks.
http://msdn.microsoft.com/en-gb/library/aa368608.aspx
Although it is for visual studio i think you can assume that it is the same in this case. It should be easy enough to make your own version. Also look in c++ builder help manual.
Ok. I solved this problem, but clicking the button in which the code is, the program don't make nothing, the labels don't change. I have the labels mentioned in the code, and the button, but i don'w know why the form don't show me the results. But thank you gor your help.
Well the code was worong, because this line:
case 1 : temp = "No root directory"; return;

don't return nothing, i change it for:
case 1 : temp = "No root directory"; break;

but now all the values are "unknown" or "0"; probably "1" is not correct...
Because the var drivetype is a string.

Can you help me?
drivetype is not a string, it is unsigned int.

Here is more information about GetDriveType: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364939(v=vs.85).aspx

DRIVE_REMOVABLE, DRIVE_FIXED, etc. are defines, like:
#define DRIVE_REMOVABLE 2
...and so on. Refer to the link i gave you.
I'm know start understanding:

unsigned int drive = StrToInt(DriveComboBox1->Drive);

with this line above i obtain the letter in the DriveComboBox, which is converter into an integer value, and this value is "drive" value.

unsigned int DriveType = GetDriveType(drive.c_str())

The objective of GetDriveType is obtaining the type of a drive, it takes a number( variable drive), but why it says drive.c_str() but this line doesn't function and says:

"Structure required on left side of . or .*"

As you can see i am a noob so keep calm with me. jaja
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <vcl.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define DRIVE_UNKNOWN 0
#define DRIVE_NO_ROOT_DIR 1
#define DRIVE_REMOVABLE 2
#define DRIVE_FIXED 3
#define DRIVE_REMOTE 4
#define DRIVE_CDROM 5
#define DRIVE_RAMDISK 6
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"
#include "Unit1.h"
#include <iostream>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        AnsiString temp;
        char Drive[256];
        AnsiString drive = DriveComboBox1->Drive; //EditDrive->Text;
        StrCopy(Drive,drive.c_str());
        StrCat(Drive,":");

        unsigned int DriveType = GetDriveType(Drive);
switch (GetDriveType)
    {
        case 1 : temp = "No root directory"; break;
        case 2 : temp = "Removable"; break;
        case 3 : temp = "Fixed"; break;
        case 4 : temp ="Remote (network) drive"; break;
        case 5 : temp = "CD-ROM"; break;
        case 6 : temp = "RAM disk"; break;
        default: temp = "Unknown"; return;
     }


Now is right!! It is identifying for example my hard disk c: as fixed.

But is the only label who was changed.

1
2
3
4
5
6
7
GetVolumeInformation (drive.c_str(), volumeinfo, 255,&VolumeSerialNumber,
        &MaximumComponentLength, &FileSystemFlags,
        FileSystemNameBuffer,255);
        if (strlen(volumeinfo) != 0)
                EditVolumeInfo->Text = volumeinfo ;
        else
                EditVolumeInfo->Text = "- no label -";


Her it returns "no labels" to me.
The objective of GetDriveType is obtaining the type of a drive, it takes a number( variable drive), but why it says drive.c_str() but this line doesn't function and says:


GetDriveType doesn't take a number but hdd name as an argument (ie, "c:\\" or "d:\\"). Then return number.

1
2
unsigned int DriveType = GetDriveType(Drive);
switch (GetDriveType)

should be
1
2
unsigned int DriveType = GetDriveType(Drive);
switch (DriveType)

If it return wrong number, you are using unrecognizable argument.

Hover cursor over the function to see what type it require.
Thank you for help me

Now i have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
        AnsiString temp;
        char Drive[256];
        AnsiString drive = DriveComboBox1->Drive; //EditDrive->Text;
        StrCopy(Drive,drive.c_str());
        StrCat(Drive,":\\");



        unsigned int DriveType = GetDriveType(Drive);
switch (DriveType)
    {
        case 1 : temp = "No root directory"; break;
        case 2 : temp = "Removable"; break;
        case 3 : temp = "Fixed"; break;
        case 4 : temp ="Remote (network) drive"; break;
        case 5 : temp = "CD-ROM"; break;
        case 6 : temp = "RAM disk"; break;
        default: temp = "Unknown"; return;
     }

and it show me Fixed for example, but the rest of the information is like before, program only recognize the drivetype.
I will thank you if you know about it.
try the fixed value, ie.:
1
2
3
4
5
6
7
GetVolumeInformation ( "c:\\" , volumeinfo, 255,&VolumeSerialNumber,
        &MaximumComponentLength, &FileSystemFlags,
        FileSystemNameBuffer,255);
        if (strlen(volumeinfo) != 0)
                EditVolumeInfo->Text = volumeinfo ;
        else
                EditVolumeInfo->Text = "- no label -";


if this function will work, then DriveComboBox1->Drive might not return what you think it does.
Well, now the function is returning "Fixed" and the serial number and the label only.
I don't know how yo change, but i take this code from C++ Builder Developers Guide. It has to be right. Jajaja!!
Topic archived. No new replies allowed.