0

我的代码在我的 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()

有时无法正确读取日期。我会得到一些字符+我想要的日期。我的部分代码已被删除(因为程序非常庞大),但希望我发送的内容会有意义。谢谢

4

1 回答 1

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
于 2013-10-15T15:31:06.583 回答