Problems with LSTRING data type (solved) 
Author Message
 Problems with LSTRING data type (solved)


Quote:

>I have fixed-record length databases with a lot of (Pascal) LString
>fields, which are difficult to read correctly.

>In Pervasive's Btrieve manual LString is listed as key type, so there
>should be some way to access data in fields correctly...

>One of databases has structure like this:
>Field1 (ZIP code) LongInt, index #3;
>Field2 (City) LString[30];
>Field3 (some data) LString[15];
>...
>Field6 (County code) Int

>If I put normal length for strings, microkernel returns error #22, and
>record length calculation gives me value of 95, and if I add 1 to
>string  lengths because I know that record length must be 101 and I
>have 6 strings (i.e. Field2 String[31]), data is a mess...
>Of course, I need data from all fields...

>Any suggestions?
>Any example code?
>Maybe example for usage of Get Next Extended?

>Btrieve is version 6.15 N on Novell server, and I use MS Visual C++
>5.0 with BTI Developer Kit...

So, I got answer from Mr. Henderson, Pervasive Software.

Here it is:

The LSTRING values in your tables have a length byte in offset [0],
then the data, beginning at offset [1].  There is no guarantee that
the remainder of the field (after the number of bytes reflected by
offset[0]) will contain binary '\0' characters, so you cannot use
printf (or any of the string functions uses for null-terminated
strings) to access the data.
An LSTRING that is 31 bytes long can only hold 30 bytes of data, in
addition to the first (length) byte.

The reason for having to do the following is that the C language has
no internal or standard library functionality for handling LSTRINGs;
they are usually used in Pascal programs.  Just remember that field[0]
is the length, and the data starts in field[1], and that the remainder
of the field's size must be considered garbage.

In this example, I have created a new temporary variable (msg), and
used strncpy() to copy the data from the LSTRING, beginning at offset
[1], copying a total of ((byte)offset [0]) bytes:

      memset(msg, '\0', sizeof(msg));
      strncpy(msg, &postaRecord.Posta_Naziv[1],
(byte)postaRecord.Posta_Naziv[0]);

I use memset() before each loading of the variable, to ensure that a
binary '\0' follows the data; strncpy() does not place a terminating
null after the data.  You might want to use something faster, such as
simply putting a single '\0' in the next byte after the strncpy() ...

Another option (since you probably have many files with the LSTRING
data in them) would be to develop a C++ class to work with them; you
could, for instance, create a conversion function to return a
null-terminated string, hiding all the strncpy() code in the class.

Now part of my code looks like this:

/////////////////////////////////////////////////////////////////
// create a temporary variable for printing
//
// make it one byte longer than the longest data value
// posta_naziv is a 31-byte LSTRING, so longest data is 30 bytes
//
/////////////////////////////////////////////////////////////////
char msg[31];
memset(msg, '\0', sizeof(msg));
printf( "  The retrieved record is:   \n\n" );
// ZIP prints okay (it is an integer)
printf( "ZIP    : %d \n", postaRecord.Posta_Sifra);
/////////////////////////////////////////////////////////////////
// and now, the routines to print LSTRINGs
/////////////////////////////////////////////////////////////////
strncpy(msg, &postaRecord.Posta_Naziv[1],
(byte)postaRecord.Posta_Naziv[0]);
printf( "City   : %s \n", msg);
// COUNTY prints okay (it is an integer)
printf( "County : %d \n", postaRecord.Posta_Opcina);
memset(msg, '\0', sizeof(msg));
strncpy(msg, &postaRecord.Posta_Drzava[1],
(byte)postaRecord.Posta_Drzava[0]);
printf( "Country: %s \n", msg);

Thank you, Mr. Henderson.

If anyone knows better solution, let us know...
Greetings,
                         Slobodan Hajdin

                         (remove nospam.)



Sat, 03 Feb 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. ATL data type for VB Object data Type

2. SQL timestamp data type - correspondency with c++ data type

3. Mapping ADO data types with C++ data types

4. OLE data types / standard data types

5. cast specifies array type (howto solve this?)

6. Help solve argument about default return type

7. memory problems need boolean data type

8. Data Type definition Problem

9. Problem with text data type

10. Oracle data type problem, porting from SQL server

11. Easy Data type problems with AddString

12. ADO data type problem

 

 
Powered by phpBB® Forum Software