-
Using a *char Buffer
Greetings!
I ran into a problem again.
I'd like to read from a file, I read the data, and I get extra characters to the end of the data! My code is the following:
Code:
void __fastcall TfrmMain::mainbetolt(TObject *Sender)
{
char *dataBuffer;
try {
int iFileHandle = FileOpen("config.dat", fmOpenReadWrite);
int iFileLength = FileSeek(iFileHandle,0,2);
int i=0;
FileSeek(iFileHandle,0,0);
dataBuffer=new char[iFileLength];
dataBuffer="";
int iBytesRead = FileRead(iFileHandle, dataBuffer,iFileLength);
if (iBytesRead != iFileLength)
{ ShowMessage("Nem megfelelő számú beolvasott byte.");}
ShowMessage(dataBuffer);
frmMunkadir->txtMunkadir->Text=dataBuffer;
FileClose(iFileHandle);
delete [] dataBuffer;
}
catch(...)
{
ShowMessage("Fájlműveleti hiba");
}
}
As you see, I define a char *Buffer here, which is a pointer of yet undefined elements. (chars)
And when I open the "config.dat" file as specified above, I get the contents of it, plus the characters "műveleti hiba" to the end of the Buffer.
The two weird things are:
-the Buffer, is LONGER with Length("műveleti hiba") characters, than I've defined it - it was defined to be the same length as the file content
-the extra characters come from THIS code! How could this be?
Please help me with this!
Thanks again
Han
-
Hi!
I have tried it, deleted the overdefinition of dataBuffer, but it's still the same except that it adds now other random characters (Cl$u in this case).
Do you know any other trustable method to read the contents of a file into a string?
Thanks
Han
-
I got fed uo with it, so I tried the dirty way :)
I put the first iBytesRead characters from th dataBuffer to a String, and used that afterwards :))))
However an another problem came into wiev in the following code:
Code:
void __fastcall TfrmAllatadat::buttonOKClick(TObject *Sender)
{
int iFileHandle;
int iFileLength;
int iBytesRead;
char *pszBuffer;
AnsiString kiirando="";
int tempint;
//Átírom az aktív doboz állatainak adatait a strukturált
//változóba
if (frmMain->box1on)
{
//Megnézem, hogy számokat írt-e be az illető, és hogy jó rangeben van-e.
try {frmMain->box1allat.hossz=StrToInt(txtBox1hossz->Text);}
catch(...)
{ShowMessage("Az 1. állat hossza hibásan lett megadva! (30-250mm)");
goto meresindvege;}
try {frmMain->box1allat.azonosito =StrToInt(txtBox1azonosito->Text);}
catch(...)
{ShowMessage("Az 1. állat azonosítója hibásan lett megadva! (1-9999)");
goto meresindvege;}
try {frmMain->box1allat.kod=StrToInt(txtBox1kod->Text);}
catch(...) {ShowMessage("Az 1. állat kódja hibásan lett megadva! (1-99)"); goto meresindvege;}
.
.
meresindvege:
}
In this piece of code (much was left out, but its damn long with the same structure -didn't feel the need to attach it) I try to make an error checking. There are some EditBoxes where I'd like the user to put an integer value.
So I use the Exception handling, to check if the entered value was valid. And when if it's not, I quit this routine (sorry for the goto), and make the user give the data once again.
The only problem is, that when the code gets to the StrToInt (with not an integer value given) , it gives an exception of EConvertError, BEFORE it reaches the catch. What can be the problem here? Is there any other Try I haven't closed with a catch? I made this check, but it all seemed ok. Every try is followed by a catch. How can I solve this?
Thanks
Han