|
Question : Read structure from binary file
|
|
I want to read an array of structures from a binary file using VB.NET.
Old VB6 code is like this:
Open FileName For Binary Access Read As #FileNumber Len = FileLength Get #FileNumber, , ArrayOfStructures(Index)
I now need to use a binaryReader I assume that can read a byte(). But how to EASILY put the byte() into the structure? I can't place all bytes just like that into the structure like in old VB6 right? I assume it does need to be done property by property using some init method or something? Something like public sub init(byteArray as byte()) IntegerProperty = cint(byteArray(index)) ' however I'd need 4 bytes of course to put into this integer. end sub
The old code used long, VB.net now uses integer. Doubles are the same in VB6 and VB.net on byte level I think? The strings are all of a set size like this in VB6 PrevTickExt As String * 4
I made this in VB.NET like this: Private m_PrevTickExt as string
Public Property PrevTickExt(ByVal i As Integer) As String Get Return m_PrevTickExt(i).PadRight(PrevTickExtLen) End Get Set(ByVal value As String) m_PrevTickExt(i) = value.Substring(1, Math.Min(PrevTickExtLen, value.Length)) End Set End Property
So basically I'd just like to easily retrieve i.e 4 values from the byte array and cast them to an integer, or 8 bytes to double or 4 bytes to string. It seems easier to do in c#, but I'm bound to VB for this. Also don't want to only make this part in C#.
|
|
Answer : Read structure from binary file
|
|
I presume that the file is a fixed length file = FileLength if so then why not this?
Dim f as Integer = FreeFile() FileOpen(f, FileName, OpenMode.Random, , OpenShare.Shared, FileLength) Dim k as Integer = LOF(f) \ Filelength Dim st(k) as MyStructure For i = 1 to k FileGet(f, st, i) Next FileClose(f)
A problem with this, which I can't solve is that you have to have Option Strict Off
|
|
|
|