Link to home
Start Free TrialLog in
Avatar of riceman0
riceman0

asked on

Reading a binary file with VB.NET

Let's try this again, a little more concisely:

I am trying to convert some code from VB6.0 to VB.NET, this particular loop populates a data structure from a binary file, and I am having a huge problem getting equivalent results.  I'll cut and paste the old code and new code below, and if anyone can tell me what I'm doing wrong, or (even better!) give me a couple of snippets, I'd be eternally graceful!  

The problems are in the GetVariable function of the second section.  Just pasted the rest for completeness.  Part of the problem this isn't simple is because the BinaryReader doesn't seem to support random access... but I'm not married to it.

Thanks!

****************************************************
old, working VB6.0 code
****************************************************

Private Type XVDataType

    sText As String * 50
    iNumPoints As Integer
    dData(50) As Double

End Type

Private Sub ButtonGo_Click()
     
     Dim X as XVDataType

     Dim f as String
     f = "C:\Documents and Settings\crice\My Documents\Active Tasks\TCFLO\Project\TCFLO\TCFLO.rst"

     X = GetVariable(f, 4)    ' calls the getvariable function with some dummy data, to test

End Sub
   
   

Private Function GetVariable(sRestartFile As String, iNumXVar As Integer) As XVDataType

    ' inumvar is 1-based
    Dim XV As XVDataType

    ' get the variable definitions
    Dim ind As Long
   
    Open sRestartFile For Binary As #1
       
    ind = 254  ' header offset

    Get #1, ind + (iNumXVar - 1) * 508 + 1, XV.iNumPoints
   
    Dim n As Integer
    For n = 0 To XV.iNumPoints - 1
        Get #1, ind + (iNumXVar - 1) * 508 + 9 + n * 8, XV.dData(n)
    Next n
   
    Get #1, ind + (iNumXVar - 1) * 508 + 459, XV.sText

    GetRestartExternalVariable = XV
   
    ' close the file and exit
ext:
    Close #1
    Exit Function

End Function


****************************************************
new, mostly not working VB.NET code
****************************************************


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Call GetVariable( _
        "C:\Documents and Settings\crice\My Documents\Active Tasks\TCFLO\Project\TCFLO\TCFLO.rst" _
        , 4)

    End Sub

    Private Function GetVariable(ByVal sRestartFile As String, ByVal iNumXVar As Integer) As XVDataType

        Dim XV As New XVDataType

        ' get the variable definitions
        Dim ind As Long = 254   ' header offset

        If System.IO.File.Exists(sRestartFile) Then

            Dim binReader As New System.IO.BinaryReader(File.Open(sRestartFile, FileMode.Open))
            binReader.ReadBytes(ind + (iNumXVar - 1) * 508)
            XV.iNumPoints = binReader.ReadByte()     ' <---- THIS WORKS, gets the right number of points
            For n As Integer = 0 To XV.iNumPoints - 1

                XV.dData(n) = binReader.ReadSingle    ' <---- this doesn't work worth a darn
                'XV.dData(n) = binReader.ReadDouble    '    <------ this didn't work either
                'binReader.ReadBytes(8)                         '    < ------ nor this        
                'Get #1, ind + (iNumXVar - 1) * 508 + 9 + n * 8, XV.dData(n)   ' <------- the equivalent VB6.0 code

            Next n

            XV.sText = binReader.ReadChars(10)      ' <------ doesn't work

            binReader.Close()

        End If

    End Function
Avatar of CooPzZ
CooPzZ
Flag of Australia image

DO you get an error message?
Does the binReader.Position  change after the readByte..?
Does the ReadByte work instead of the ReadSingle at the first problem.?
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sancler
Sancler

>>
What you have read is the first 4 BITS of the data ...
<<

should be

>>
What you have read is the first BYTE of the data ...
<<

Roger
Roger brings up a good point about the size of Ints being different in VB.Net.

Important questions:

    Are you reading the OLD file created by VB6?

    ...or are you attempting to read a NEW file that was created by VB.Net code with new .Net types (and consequently new different sizes)?



Have you considered using XML or possibly Binary Serialization instead of Structures?
Avatar of riceman0

ASKER


To answer the questions:

* no error messages, just wrong values
* correct, the data in the file is not ordered like the order of the structure -- maybe a little misleading; it is # points, data, text
* the VB.NET class is reading the exact same file as the VB6.0 class, they were both produced by the same ancient DOS program that this is post-processing  :)

I will try the suggestions in the next couple of hours, thanks!