ASSOC and structures don't mix [message #11105] |
Thu, 12 March 1998 00:00 |
Brian Jackel
Messages: 34 Registered: January 1998
|
Member |
|
|
Recently some people have remarked that ASSOC is a useful
tool for I/O. While this is true, there is a subtle problem
when reading structures from a file that was not written
using ASSOC. The following script demonstrates what I'm
talking about. Of course, the obvious workaround is to
write the files using ASSOC as well, but that is not always
an option.
;
;IDL script to demonstrate why it's dangerous
;to use ASSOC with structures.
;
;Make a test structure, write it out with some
;values in it.
;
test= {PROBLEM, a:0b, b:FLTARR(3)}
OPENW,lun,'test.dat',/GET_LUN
FOR indx=0,3 DO BEGIN
test.a= indx
test.b= [indx,indx+0.5,indx+1.5]
WRITEU,lun,test
ENDFOR
CLOSE,lun
;Reading it back in works like a charm...
;
OPENR,lun,'test.dat',/GET_LUN
READU,lun,test & PRINT,test
READU,lun,test & PRINT,test
READU,lun,test & PRINT,test
;...but the results from ASSOC aren't so good
;
dat= ASSOC(lun,{PROBLEM})
PRINT,dat(0)
PRINT,dat(1)
PRINT,dat(2)
;because IDL is zero padding the structure to
;some multiple of 8 bytes (?). There's actually
;only 13 bytes (assuming 4 byte floats).
;
HELP,/STR,{PROBLEM}
END
--
Brian Jackel
|
|
|