Read an array from a binary file

kcwallace

Well-known member
Joined
May 27, 2004
Messages
172
Location
Irvine, CA
I have a VB6 application that writes a 2 dimensional array (40,1024) to a file using

Code:
FileNum2=freefile
Open FilePath For Binary As #FileNum2
Put #FileNum2, , VoltArr
Close #FileNum2

Where VoltArr is a 40 x 1024 array of double type.

I know need to open that file using .NET 2003.

I need to load the entire array at once just like I did in VB6 using

Code:
Dim FileNum As Integer
FileNum = FreeFile
Open FName For Binary As #FileNum
Get #FileNum, , ImageArr Populate array
Close #FileNum

Can someone point me in the right direction?
 
Last edited by a moderator:
I found my own answer

Code:
Dim FNum As Int16 = FreeFile()
FileOpen(FNum, FName, OpenMode.Binary)
FileGet(FNum, ImgArr)
FileClose(FNum)
 
.Net introduces streams to Visual Basic. I hear rumors that streams are faster (I dont doubt it, since under the hood, FileOpen, FileGet, and FileClose probably use FileStreams), and once you get used to them, it is quite possible that you might like the way they work better. The Visual Basic file access functions are considered "bad" and "vb-ish." They arent exactly C# friendly (it might not be a problem for you, but it could become a problem when a C# programmer wants to help you with code or you search google for help with file access and come up with C# code). Ultimately it is a matter of preference, but I thought I would just give you more options and broaden your horizon and all that happy stuff.
 
I tried to get the filesteam to work, I was successful, but never without having to loop through the entire array which is very time consuming.

I would love to make it work without the old code.
 
You can read a whole bunch of bytes into a buffer (in this case our buffer could be an array of bytes).
Code:
        Lets load 100 bytes of data into memory in one read operation
        Dim MyStream As New IO.FileStream("MyFile", IO.FileMode.Open, IO.FileAccess.Read)
        Dim MyData As Byte() = New Byte(99) {}
        MyStream.Read(MyData, 0, MyData.Length)
        MyStream.Close()
 
Back
Top