Getting Types that were Put from an Array 
Author Message
 Getting Types that were Put from an Array

I am trying to find any how to's for "Get" ing data from an binary output
file that was Put into the file as an array.  For example:

The put procedure reads like this:

    N = FreeFile
    Open VariableFileName For Binary Access Write As #N

        Put #N, , UserType1

        For I = 1 To Variable1Counter
            Put #N, , UserType2(I - 1)
        Next I

        For I = 1 To Variable2Counter
            Put #N, , UserType3(I - 1)
        Next I

    Close #N

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.

Matt



Sun, 08 Aug 2004 23:20:12 GMT  
 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



Mon, 09 Aug 2004 00:20:11 GMT  
 Getting Types that were Put from an Array
That was excellent advice.  I appreciate that.  I will incorporate the ideas
into the subroutines.  Thank you for answering so quickly as well.  I must
have misread the descriptor piece of the "Get" help file, but from what you
have mentioned, it clears up allot of questions on Binary I/O.  Thank you  I
especially liked that idea about the file signature and version number.  It
should help immensely in the coming years.

Matt



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



Mon, 09 Aug 2004 02:04:37 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Getting the basic [Type] of an array [Type[]]

2. Getting file info using a file list box and putting in an array

3. Declaring an array of pointers in Visual Basic (and putting and getting data from it)

4. Declaring an array of pointers in Visual Basic (and putting and getting data from it)

5. Convert array of value type to array of reference type

6. where to put Type, End Type in form?

7. Type Mismatch - Array of Type Long

8. How to pass an integer array/userdef type array into an Oracle Stored procedure

9. VB6: How to initialize a variable as a array structure / structure (type) array (see example)

10. why am I getting this error??

11. Strongly Typed - what am I not catching?

12. Why am I getting this message?

 

 
Powered by phpBB® Forum Software