
Getting Types that were Put from an Array
Quote:
> Keeping in mind that both arrays are dynamic, and that the user defined
> types are not preset as to their length (which is why I chose Binary in
the
> first place), how would I "Get" them from the file once they were wrote?
I
> understand that VB writes a 10bit descriptor for the array, but once
again,
> how would find this discriptor and use it? The only way I can think of is
> to read the file bit by bit, but hopefully there are better ways.
No, array descriptors are only written (or read) if the file is open for
Random. If it's open for Binary access, there is no array descriptor. You
must provide the equivalent of an array descriptor yourself. I assume that
Microsoft did this so that VB could read and write binary files of any
format, so it writes and reads everything "raw", and free of VB-specific
data structures, like array descriptors.
Making the contents of a binary file comprehensible to the program that's
going to be reading it is the programmer's responsibility. A binary file is
"self-descriptive" only to the extent you make it that way. It isn't
automatic.
When writing dynamic arrays into Binary files, I always write the current
array size first as an Integer or Long, followed by the array itself, which
can be written in its entirety in a single Put statement. To read the file,
read the array size first, do a ReDim to create the dynamic array, and then
do a Get to read it. Arrays can also be read and written in a loop, as you
did, but it's faster to Put or Get the whole array at once, I've found.
I also make a point of writing some sort of "file version" marker at the
beginning of the file, so the reading program can check it and accommodate
different file formats as they change. It's also a good idea to include a
"signature" string at the beginning of the file, so the program doesn't try
to make sense of a file written by the wrong program.
Binary files are the most efficient, but they must be designed carefully, to
avoid later problems with inflexibility.
--
Russ Holsclaw