Solved

Problem with character strings in a delimeted buffer class

Posted on 2004-10-13
9
229 Views
Last Modified: 2012-06-21
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:");      
0
Comment
Question by:Tim_Heldberg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12306253
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?
0
 
LVL 86

Expert Comment

by:jkr
ID: 12308326
Have you tried running that under a debugger?
0
 
LVL 4

Author Comment

by:Tim_Heldberg
ID: 12321991
Here is where it is first crashing

    memcpy (field,&Buffer[start] , len);  in the unpack function
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 12322073
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?

0
 
LVL 4

Author Comment

by:Tim_Heldberg
ID: 12326297
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 12329204
>> 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?
0
 
LVL 4

Author Comment

by:Tim_Heldberg
ID: 12329290
NextByte is an inheirited member from an iobuffer class that is used for different types of buffers, at this point it is four
0
 
LVL 13

Expert Comment

by:SteH
ID: 12336781
And what is the value of BufferSize. And how long is Buffer? (I guess BufferSize) Can you post the address of field and Buffer.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12341641
Actually, it'd be more helpful to post the whole code so one can run it using a debugger :o)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question