Solved

Problem with character strings in a delimeted buffer class

Posted on 2004-10-13
9
228 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
Industry Leaders: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 219
Unresolved External Symbols 3 154
How to gracefully close the c++ 11 thread? 3 157
HTTPSendRequest with WinINet delays on first call 11 119
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

732 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