View Poll Results: What do you think of pointer arithmetics?

Voters
10. You may not vote on this poll
  • Pointers are fun! I use them a lot.

    3 30.00%
  • I have no problems with pointers. They are quite useful to me sometimes.

    5 50.00%
  • You only have to understand it once. It takes awhile, but I'm done with it and I use them when necessary.

    1 10.00%
  • No, God saved me from this.

    1 10.00%

Using a *char Buffer

This is a discussion on Using a *char Buffer within the C++ Programming forums, part of the General Programming Boards category; Greetings! I ran into a problem again. I'd like to read from a file, I read the data, and I ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    105

    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

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    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

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 10:12 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21