From: Larry Serflaten on

"Mike Williams" <Mike(a)WhiskeyAndCoke.com> wrote

> > 1) The array needs to be (re)dimmed before I load it.
>
> The Get statement loads data from a file into a variable (or into an array
> of variables) and it loads exactly the required number of bytes to fill up
> that variable (or array). You can use Get to load a single variable or a
> single array or a single UDT, or you can use it to load lots of variables
> and arrays and UDTs from the same file. In order that it can do this you of
> course need to tell it each time you use it how many bytes you want it to
> load, which in this specific case you do by Dimensioning the array to the
> required size before using the Get statement. Once the Get statement has
> loaded the required data into the variable (or array) it then leaves the
> "file byte pointer" pointing the file byte that immediately follows the data
> it has just loaded.


You couldn't just say yes or no, could you? The short answer is no,
the array does not need to be (re)dimensioned prior to reading in the data.

While that may be in the explaination you posted, somewhere, it sure
wasn't any too obvious.....

The guy asked about saving a single array, and you say; yeah, you can do
a bunch if you do it a certain way.... What's up with that?

LFS


From: Alexandros Peropulous on
My experience and what I read from Mike's answer was that I do need to
"resize" the array to the needed size before calling "get", and I need
to store the "length"/ "UBound" of the array somewhere, preferrably in
the file itself.

Not correct?

Larry Serflaten:
> "Mike Williams"<Mike(a)WhiskeyAndCoke.com> wrote
>
>>> 1) The array needs to be (re)dimmed before I load it.
>>
>> The Get statement loads data from a file into a variable (or into an array
>> of variables) and it loads exactly the required number of bytes to fill up
>> that variable (or array). You can use Get to load a single variable or a
>> single array or a single UDT, or you can use it to load lots of variables
>> and arrays and UDTs from the same file. In order that it can do this you of
>> course need to tell it each time you use it how many bytes you want it to
>> load, which in this specific case you do by Dimensioning the array to the
>> required size before using the Get statement. Once the Get statement has
>> loaded the required data into the variable (or array) it then leaves the
>> "file byte pointer" pointing the file byte that immediately follows the data
>> it has just loaded.
>
>
> You couldn't just say yes or no, could you? The short answer is no,
> the array does not need to be (re)dimensioned prior to reading in the data.
>
> While that may be in the explaination you posted, somewhere, it sure
> wasn't any too obvious.....
>
> The guy asked about saving a single array, and you say; yeah, you can do
> a bunch if you do it a certain way.... What's up with that?
>
> LFS
>
>

From: Nobody on
"Alexandros Peropulous" <peropero(a)gmail.com> wrote in message
news:eKgz1wZGLHA.3468(a)TK2MSFTNGP05.phx.gbl...
> My experience and what I read from Mike's answer was that I do need to
> "resize" the array to the needed size before calling "get", and I need to
> store the "length"/ "UBound" of the array somewhere, preferrably in the
> file itself.

Correct. This is explained in MSDN under "Get statement".


From: Larry Serflaten on

"Alexandros Peropulous" <peropero(a)gmail.com> wrote
> My experience and what I read from Mike's answer was that I do need to
> "resize" the array to the needed size before calling "get", and I need
> to store the "length"/ "UBound" of the array somewhere, preferrably in
> the file itself.
>
> Not correct?

While keeping a header for version and size information is a good idea,
it is not a requirement.

Step through this example below to see you do not need to know the
size nor the number of dimensions, if that is a criteria....

(As I said earlier, Open, Put, Get, and Close is all you need)

LFS

-----------------

Private Sub Form_Load()
Const TestFile = "D:\Temp\Test.dat"
Dim dataOut As Variant
Dim dataIn As Variant ' This variable is not sized

' Store values
ReDim dataOut(0 To 1, 0 To 1)
dataOut(0, 0) = "zero"
dataOut(0, 1) = "one"
dataOut(1, 0) = 10.1
dataOut(1, 1) = True


' Disk access
Open TestFile For Binary As 1
' Save to file
Put #1, 1, dataOut
' Read from file
Get #1, 1, dataIn
Close


' Values are stored in a new array, ready for use
Debug.Print dataIn(0, 0)
Debug.Print dataIn(0, 1)
Debug.Print dataIn(1, 0)
Debug.Print dataIn(1, 1)

End Sub


From: Nobody on
"Nobody" <nobody(a)nobody.com> wrote in message
news:%23nFno6ZGLHA.5448(a)TK2MSFTNGP06.phx.gbl...
> "Alexandros Peropulous" <peropero(a)gmail.com> wrote in message
> news:eKgz1wZGLHA.3468(a)TK2MSFTNGP05.phx.gbl...
>> My experience and what I read from Mike's answer was that I do need to
>> "resize" the array to the needed size before calling "get", and I need to
>> store the "length"/ "UBound" of the array somewhere, preferrably in the
>> file itself.
>
> Correct. This is explained in MSDN under "Get statement".
>

Also, if you put the array in UDT and give the UDT var to Put/Get, then VB
would save the array descriptor for you and do ReDim when "Get" is called.
example code:

Option Explicit

Private Type TByte
a() As Byte
End Type

Private Sub Form_Load()
Dim f As Integer
Dim udt As TByte

ReDim udt.a(1 To 3)

udt.a(1) = 65
udt.a(2) = 66
udt.a(3) = 67

f = FreeFile
Open "C:\vbtest.txt" For Binary As f
Put f, , udt
Close f

Erase udt.a

f = FreeFile
Open "C:\vbtest.txt" For Binary As f
Debug.Print "File size is " & LOF(f)
Get f, , udt
Close f
Debug.Print LBound(udt.a), UBound(udt.a)
Debug.Print udt.a(1)
Debug.Print udt.a(2)
Debug.Print udt.a(3)

End Sub


Output:

File size is 13
1 3
65
66
67