Reading Blockwritten typed files using C 
Author Message
 Reading Blockwritten typed files using C

Hi, I've been trying to use C to read some database/data library files I
created using Blockwrite in delphi 3.

I write the data to my library using the following record format :

type
  LibraryFileRecord = record
        Index : integer;
        Name : string[64];
        ppm : single;
        Date : string[30];
        Data : array[0..2047] of single;
end;

and I write this with:
        BlockWrite(LF, LibFileRec, 1, NumWritten);

I have been trying to decipher how Borland/Pascal stores the information
in a typed file by using a hex editor and have made some progress.  I
can read up to the data array so far, but I would prefer to have some
sort of a known format to follow to figure this out.  What I have
noticed is that after the "Index" integer there is a integer "space" and
then a byte containing the number of valid characters in the "Name"
string that follows.  For example if the record contained
Index = 1, Name = JUICE, ...

The first few bytes of the file would look like this :

01 00 00 00 05 4A 55 49 43 45 .....

The 4A .. 45 is JUICE of course.   I though there would be a simple two
byte gap between record entries and then I found that there are 4 bytes
between the string[64] and the single (ppm) and no gap between the
single (ppm) and the string[30] (there is the 1 byte containing the
number of valid characters in the string[30].  Rather than find more
exceptions to my 'rule', I would rather have the actual format
pascal/delphi uses to write these files then I can set up a simple
struct in C to read it back.

 Any ideas?

Marcell

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Blockwritten typed files using C

Quote:

> Hi, I've been trying to use C to read some database/data library files I
> created using Blockwrite in Delphi 3.

> I write the data to my library using the following record format :

> type
>   LibraryFileRecord = record
>         Index : integer;   has 4 bytes in Delphi 3
>         Name : string[64]; length byte + 64 chars
>         ppm : single;      4 bytes real format
>         Date : string[30]; length byte + 30 chars
>         Data : array[0..2047] of single;  2048*4 bytes
> end;

Delphi is a 32-bit program and it makes 32-bit integers, similar
to Longint in older Turbo Pascal. There is a choice in Delphi:
ShortInt.

Regards,
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation

http://www.geocities.com/~franzglaser/ http://members.xoom.com/f_glaser/



Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Blockwritten typed files using C
Delphi puts padding in between fields of a record, if necessary, to improve
performance. To simplify your job you should declare the record as packed,
e.g.
    LibraryFileRecord = packed record . . .
D3 integers are 4 byte values. Short strings have a length byte followed by
the declared length of bytes. Single is a four byte value. The array of
single will simply be 2048 four byte entries, no padding etc.

Check out the Internal Data Formats section of the help for further details.

Quote:

> Hi, I've been trying to use C to read some database/data library files I
> created using Blockwrite in Delphi 3.

> I write the data to my library using the following record format :

> type
>   LibraryFileRecord = record
> Index : integer;
> Name : string[64];
> ppm : single;
> Date : string[30];
> Data : array[0..2047] of single;
> end;

> and I write this with:
> BlockWrite(LF, LibFileRec, 1, NumWritten);

> I have been trying to decipher how Borland/Pascal stores the information
> in a typed file by using a hex editor and have made some progress.  I
> can read up to the data array so far, but I would prefer to have some
> sort of a known format to follow to figure this out.  What I have
> noticed is that after the "Index" integer there is a integer "space" and
> then a byte containing the number of valid characters in the "Name"
> string that follows.  For example if the record contained
> Index = 1, Name = JUICE, ...

> The first few bytes of the file would look like this :

> 01 00 00 00 05 4A 55 49 43 45 .....

> The 4A .. 45 is JUICE of course.   I though there would be a simple two
> byte gap between record entries and then I found that there are 4 bytes
> between the string[64] and the single (ppm) and no gap between the
> single (ppm) and the string[30] (there is the 1 byte containing the
> number of valid characters in the string[30].  Rather than find more
> exceptions to my 'rule', I would rather have the actual format
> pascal/delphi uses to write these files then I can set up a simple
> struct in C to read it back.

>  Any ideas?

> Marcell

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Blockwritten typed files using C


Quote:
> Delphi puts padding in between fields of a record, if necessary, to
improve
> performance. To simplify your job you should declare the record as
packed,
> e.g.
>     LibraryFileRecord = packed record . . .
> D3 integers are 4 byte values. Short strings have a length byte
followed by
> the declared length of bytes. Single is a four byte value. The array
of
> single will simply be 2048 four byte entries, no padding etc.

> Check out the Internal Data Formats section of the help for further

details.

Thanks for the tip on using a packed record.  I know the sizes of the
elemnets, but I also noticed there was padding and I was trying to
figure out the rhyme/reason.  Unfortunately it is not simple (see help
file on Record Types).  I can read the first 7 'singles' from my data
array and then there is a 1 byte pad and then I can read the rest again
(at least for about 60 values, I haven't checked all yet).  Appararently
there is padding within an array as well.  I found some of this
alignment padding information under "Internal Data Formats, Record
Types/ Arrays".  Arrays can have the same padding as their individual
elements.

To the fellow who said to use a ShortInt :  A ShortInt is a signed byte,
a smallint is a signed word.  You have them confused.  "Real" in Delphi
is 6 bytes, not 4 (that's a single, ie float in C).

I could probably get it to work with the padding, but it will be much
easier without.

Thanks,
Marcell

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Reading Blockwritten typed files using C

2. Reading typed files

3. Reading database-type files

4. Need to read type 0 PCX file in TP30

5. Read lots of records at the same time from a typed file

6. Reading a QNX ZOS formatted file from win95/NT using Pascal

7. APPEND to a file of record typed file

8. How to file/record lock a typed file.

9. Text file to Type File!

10. (FPU) EXTENDED type read by C program

11. Reading Enumerated Types

12. Reading Diff. Var types

 

 
Powered by phpBB® Forum Software