Could anyone out there please help me with reading packed records? I have
read through all the books I have and combed the archives for the answers I
need with no luck.
Each record in my DB needs to store a C struct with fixed sized variables
followed by 3 variable length strings which I am trying to pack into one
packed string.
My function that writes the data is:
// MyRecType is the name of the struct that first gets writen.
// get size of MyRecType like... StructSize = sizeof(MyRecType);
// Calculate length of each string and add 1 for null byte. like...
String1Len = (StrLen(String1) + 1);
// add up size of struct and all the lengths of the 3 strings and put in
UInt16 RecSize
Then write record like this:
MemHandle NewRecordH;
MemPtr RecPtr;
NewRecordH = DmNewRecord(DBRef,&Location,RecSize);
RecPtr = MemHandleLock(NewRecordH);
// Write the struct to the record.
DmWrite(RecPtr,0,&prefs.RecTypePtr,StructSize); // RecTypePtr is a
copy of MyRecType struct.
// Write the 3 strings just after the struct.
DmWrite(RecPtr,StructSize,prefs.String1,String1Len);
DmWrite(RecPtr,(StructSize + String1Len),prefs.String2,String2Len);
DmWrite(RecPtr,(StructSize + String1Len +
String2Len),prefs.String3,String3Len);
MemHandleUnlock(NewRecordH);
DmReleaseRecord(DBRef,Location,true);
I THINK this part is ok.
But my trouble is from reading those strings back when I want to look at the
records and print the strings on screen.
I have tried examples out of books but couldn't get them working so I tried
something like this.
char* ItemTxt;
MemHandle RecH;
MemPtr RecP;
RecH = DmQueryRecord(DBRef,RecID);
// lock handle and cast pointer as a char* type
RecP =(char*) MemHandleLock(RecH);
// move the pointer over past the struct to the start of the packed strings.
RecP = (RecP + sizeof(MyRecType));
// copy String1 from DB to the ItemTxt variable.
StrCopy(ItemTxt,RecP);
// unlock pointer
MemPtrUnlock(RecP);
This all compiles but when I get to the above code in the program I get NULL
strings passed errors as well as my app just wrote to low memory.
Could someone please guide me on the best way on reading packed strings from
DB records?
Thanks so much for all the help in advance.
Regards,
Jim
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/