难以读写二进制文件

时间:2013-10-15 15:05:00

标签: vb.net binary

我的代码适用于我的64位计算机,但在32位计算机上运行时遇到以下错误(有时候代码工作正常):

InvalidCastException未处理

接下来的几行提供了我的代码:

要写入二进制文件:

Dim  writeStream = New FileStream(path, FileMode.Open)
Dim BinReader As BinaryReader

next_byte = Len(CStr(time)) + Len("EndOfHeader") + 16 + 2
first_time = True

BinWriter = New BinaryWriter(writeStream)

For i = 0 To Form1.signals.Length - 1
    If IsNothing(Form1.signals(i)) = False Then
        'once for each test
        BinWriter.Write(Int(Form1.signals(i).picodata.GetLength(0) - 1)) 'n for each signal in test
        BinWriter.Write(Form1.signals(i).picodata(1, 0) - Form1.signals(i).picodata(0, 0)) 'timestep
        BinWriter.Write(next_byte) 'position of start of test
        BinWriter.Write(CStr(time))
        Exit For
    End If
Next

BinWriter.Write("EndOfHeader")

For i = 0 To Form1.signals.Length - 1
    If IsNothing(Form1.signals(i)) = False Then
        BinWriter.Write(i)
            For j = 1 To Form1.signals(i).picodata.GetLength(0) - 1
                BinWriter.Write(Form1.signals(i).picodata(j, 1))
        Next
    End If
Next

BinWriter.Close()

阅读:

Dim readstream As FileStream
Dim end_test As Integer
Dim Index As Integer
Dim BinReader As BinaryReader
Dim end_head as Boolean=false        
Dim count as integer=0

selected_test=0

ReadStream = New FileStream(readFileName, FileMode.Open)
BinReader = New BinaryReader(ReadStream)

'read header
While end_head = False
    Try
        pos_old = ReadStream.Position
        try_string = BinReader.ReadString
        If try_string = "EndOfHeader" Then
            Exit While
        Else
            ReadStream.Position = pos_old
        End If
    Catch ex As Exception
        ReadStream.Position = pos_old
    End Try


    'this approach allows for flexibility 
    number_arr(count) = BinReader.ReadInt32
    TimeStep_arr(count) = BinReader.ReadDouble
    position_arr(count) = BinReader.ReadInt32
    time_arr(count) = CDate(BinReader.ReadString)
    count += 1
End While

'read in data
While readstream.Position <> read_stream.length
    ReDim PicoData(number_arr(selected_test), 1)
    Index = BinReader.ReadInt32
    n = number_arr(selected_test)

    For i = 1 To n
        PicoData(i, 1) = BinReader.ReadDouble
        PicoData(i, 0) = TimeStep_arr(selected_test) * i
    Next

    ReDim TimeShort(Int(n / 20))
    ReDim FiltVoltsShort(Int(n / 20))
    ReDim FiltVelShort(Int(n / 20))
    ReDim RawVoltsShort(Int(n / 20))

    'generate new reading here
    Call FourierFilter(PicoData, 0)

    signals(Index) = New reading(Index, TimeShort, RawVoltsShort, FiltVelShort, FiltVoltsShort, Points_Store(ii, 2), Points_Store(ii, 1), DataChart, VelocityChart, SelectedTimeBox, SelectedVelocityBox, True, PicoData)

End While


BinReader.Close()
readstream.Close()

偶尔无法正确读取日期。我会得到一些角色+我想要的日期。我的部分代码已被删除(因为程序非常庞大)但希望我发送的内容会有所帮助。感谢

1 个答案:

答案 0 :(得分:0)

您的第一步应该是找到一个尽可能简单的可重现的测试用例,每次都可以让它失败。然后,更容易识别问题的根源。

这样的代码可能会帮助您缩小BinaryWriter在不同平台上编码字符串的方式。

Dim content As String = New String("!"c, 255)
Using outStream As New System.IO.MemoryStream()
   Using bw As New System.IO.BinaryWriter(outStream)
      bw.Write(content)
      bw.Flush()
      outStream.Flush()
      Console.WriteLine("I am a " & If(Environment.Is64BitProcess, "64", "32") & _
          "-bit process")
      Console.WriteLine("I generated a string of {0} characters into a stream " & _
          "of {1} bytes", content.Length, outStream.Length)
   End Using
End Using