Link to home
Start Free TrialLog in
Avatar of Tim_Heldberg
Tim_Heldberg

asked on

Problem with character strings in a delimeted buffer class

I am working on a class that takes a person object, then packs it to a buffer and writes it to a file.  The problem is that when I try to read it back into the buffer from the file, I get a runtime error.  Here is a copy of the significant functions:

Any help would be appreciated


Here are the pack and unpack functions in my person class:


int Person::Pack (IOBuffer & Buffer) const
{// pack the fields into a FixedFieldBuffer,
 // return TRUE if all succeed, FALSE o/w
      int numBytes;
      Buffer . Clear ();
      numBytes = Buffer . Pack (FirstName);
      if (numBytes == -1) return FALSE;
      numBytes = Buffer . Pack (LastName);
      if (numBytes == -1) return FALSE;
      numBytes = Buffer . Pack (IDNumber);
      if (numBytes == -1) return FALSE;

      return TRUE;
}


int Person::Unpack (IOBuffer & Buffer)
{

      Clear ();
      int numBytes;
      numBytes = Buffer . Unpack (FirstName);
cout << numBytes;
      if (numBytes == -1) return FALSE;
      FirstName[numBytes] = 0;
      numBytes = Buffer . Unpack (LastName);
      if (numBytes == -1) return FALSE;
      LastName[numBytes] = 0;
      numBytes = Buffer . Unpack (IDNumber);
      if (numBytes == -1) return FALSE;
      IDNumber[numBytes] = 0;
      return TRUE;
}



Here is the unpack from  delimited buffer:

int DelimFieldBuffer :: Pack (const void * field, int size)
// set the value of the next field of the buffer;
// if size = -1 (default) use strlen(field) as Delim of field
// return number of bytes packed, -1 if error
{
      // look for delimeter in field!
      short len; // length of string to be packed
      if (size >= 0) len = size;
      else len = strlen ((char*)field);
      if (len > (short)strlen((char*)field)) // field is too short!
            return -1;
      int start = NextByte; // first character to be packed
      NextByte += len + 1;
      if (NextByte > MaxBytes) return -1;
      memcpy (&Buffer[start], field, len);
      Buffer [start+len] = Delim; // add delimeter
      BufferSize = NextByte;
      return len;
}

int DelimFieldBuffer :: Unpack (void * field, int maxBytes)
// extract the value of the next field of the buffer
// return the number of bytes extracted, -1 if error
{
      
      int len = -1; // length of packed string
      int start = NextByte; // first character to be unpacked
      for (int i = start; i < BufferSize; i++)
            if (Buffer[i] == Delim)
                  {len = i - start; break;}
      if (len == -1) return -1; // delimeter not found
      NextByte += len + 1;
      if (NextByte > BufferSize) return -1;
      // check for maxBytes

      memcpy (field,&Buffer[start] , len);      
      if (maxBytes > len || maxBytes == -1)
  ((char*)field) [len] = 0; // zero termination for string
      return len;
}



and here is the test:



      // Test writing
      //Buff . Print (cout);
      ofstream outfile (myfile,ios::out);
      result = Buff . WriteHeader (outfile);
      cout << "write header "<<result<<endl;
      person1 . Pack (Buff);
      //Buff . Print (cout);
      recaddr1 = Buff . Write (outfile);
      cout << "write at "<<recaddr1<<endl;
      person2. Pack (Buff);
      //Buff . Print (cout);
      recaddr2 = Buff . Write (outfile);
      cout << "write at "<<recaddr2<<endl;
      person3. Pack (Buff);
      //Buff . Print (cout);
      recaddr3 = Buff . Write (outfile);
      cout << "write at "<<recaddr3<<endl;
      outfile . close ();

      // test reading

      ifstream infile (myfile, ios::in);
      result = Buff . ReadHeader (infile);

      cout <<"read header "<<result<<endl;

Buff . DRead (infile,recaddr3);
      person . Unpack (Buff);
      person . Print (cout, "First record:");
      Buff . DRead (infile, recaddrw);
      person . Unpack (Buff);
      person . Print (cout, "Second record:");      
Avatar of Member_2_1001466
Member_2_1001466

What is the runtime error and at which line does it occur?

From the code snippets it is not clear ie how long Buffer is. There might be a write out of bounds in Pack.
If Buffer in Unpack is a file buffer why can you be sure that no record will start in one buffer but end in another?
Avatar of jkr
Have you tried running that under a debugger?
Avatar of Tim_Heldberg

ASKER

Here is where it is first crashing

    memcpy (field,&Buffer[start] , len);  in the unpack function
OK, then let's take a look at that code:

    int len = -1; // length of packed string
    int start = NextByte; // first character to be unpacked  <---- where does 'NextByte' come from?
    for (int i = start; i < BufferSize; i++)
         if (Buffer[i] == Delim)
               {len = i - start; break;}
    if (len == -1) return -1; // delimeter not found
    NextByte += len + 1;
    if (NextByte > BufferSize) return -1;
    // check for maxBytes

    memcpy (field,&Buffer[start] , len);    // what are len and start at this point? What is 'field' pointing to?

NextByte is the index of the next byte to be unpacked

len is three, which is the length of the next field to be unpacked
and field is pointing to the next person object field to be unpacked into
>> NextByte is the index of the next byte to be unpacked

Yes, but is it a member, a globa or a static variable? And, is it valid at that moment?
NextByte is an inheirited member from an iobuffer class that is used for different types of buffers, at this point it is four
And what is the value of BufferSize. And how long is Buffer? (I guess BufferSize) Can you post the address of field and Buffer.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial