April 26, 2024, 05:57:37 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


IWSTRINGS in binary records

Started by jayelbee, April 26, 2008, 10:43:32 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

jayelbee

I am trying to read a binary file with a known record length of 114 bytes.  There are mixed unicode string filelds and binary numeric fields in the record.  I can't seem to get this program to execute.  Problems - the computed length of the record comes out to 136 bytes and the contents of the records print out as garbage.  What am I missing? Thanks.

' read a file created in Approach and create INT64 values for dbl values
AUTODEFINE "OFF"

TYPE AmtType
  DEF lbl1[7]     :iwstring                 '14
  DEF samt[10] :iwstring                 '20
  DEF lbl2[4]     :iwstring                 '8
  DEF damt      :Double                   '8
  DEF lbl3[5]     :iwstring                 '10
  DEF lamt       :INT                      '4
  DEF lbl4[5]    :iwstring                 '10
  DEF camt     :INT64                    '8
  DEF lbl5[6]    :iwstring                 '12
  DEF amt64    :INT64                    '8
  DEF lbl6[6]    :iwstring                 '12  = 114 which is known length of record
ENDTYPE
DEF amtrec as AmtType

int ec, flen, nrec, i, rl
string fnam

BFILE myfile
fnam="C:\\database\\dev\\NumFormat\\numformats.dat"

OPENCONSOLE
IF (OPENFILE(myfile, fnam, "R+")=0)
   flen=LEN(myfile) ' known length 2850
   rl=LEN(amtrec)   ' computed record length is 136
   nrec=flen/114    ' known record length is 114
   PRINT amtrec.lbl1
   PRINT amtrec.lbl2
   PRINT "Fsz: ",flen, "rcdsz: ", rl, "  Nrec: ", nrec
   FOR i = 1 to nrec
     GET myfile,i,amtrec
     PRINT amtrec.lbl1,"  ", amtrec.lbl2, "  ", amtrec.lbl3,"  ", amtrec.lbl4        ' this prints nothing
     PRINT "STR: ", amtrec.samt, "  DBL: ", STR$(amtrec.damt), "  $: ", amtrec.camt  ' this prints garbage for numerics
   NEXT i
   CLOSEFILE myfile
ENDIF
DO
UNTIL INKEY$<>""
CLOSECONSOLE
END



Ionic Wind Support Team

All types (structures) have padding and alignment of elements (known as packing)  which is used by the compiler for more efficent access in memory.  Emergence uses the same packing and alignment specified for ANSI compilers. When reading/writing to a disk file you can override the default packing of 8 using the optional argument of the TYPE statment...


TYPE AmtType, 1

The "1" tells the compiler not to align data in dword boundries and don't pad odd numbers of bytes with NULLs.  Note that if you are going to be using a large array of these then it is best to have two UDT types.  One for disk access and another used for arrays, copying between the two when needed.  I won't go into the nasty details of Intel processors and memory alignment.

Paul.
Ionic Wind Support Team