0

我在 Option Strict 开启时出现从二进制文件读取结构数据的功能有问题。

Public Function arh_setup(ByVal rw As Boolean) As Integer
    Dim retval As Integer = 0
    Dim fnum As Integer = FreeFile()
    Dim temp As ValueType = CType(New aSetup, ValueType)
    Try
        FileOpen(fnum, setup_file, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Shared, Len(asT))
        If rw Then
            FilePut(fnum, asT, 1)
        Else
            FileGet(fnum, temp, 1)
        End If
        FileClose(fnum)
    Catch ex As Exception
        retval = -1
    End Try

' Now is question here
' How to get data from temp to asT?? 
EDIT:
Solution is asT = CType(temp, aSetup)

    temp= Nothing        
    Return retval
End Function

asT 是全局声明的 aSetup (结构)类型的变量,它应该从包含(正确)读取数据的 temp 中获取数据。

问题是如何将数据从局部临时变量复制到全局 asT 变量,最好不循环(逐字节)或在不可能的情况下循环?

4

1 回答 1

1

尝试铸造:

asT = CType(temp, aSetup)

Nothing另外,在退出方法之前不要设置局部变量,这完全没有意义。并且您应该使用.NET 的FileStreams 等而不是VB6 兼容方法来读取文件。

于 2012-12-13T23:46:45.830 回答