解析一个二进制文件,自定义了一个结构,准备用这个结构读取文件,但是发生错误。
结构定义如下:
Public Structure UserData
Public bytUserID() As Byte
Public bytCard1Kind As Byte
Public bytCard1PW() As Byte
Public bytCard2Kind As Byte
Public bytCard2PW() As Byte
Public bytUserProperty As Byte '属性
Public bytDoorPower As Byte
Public bytPassPower() As Byte '通行権限番号
Public bytStartTime() As Byte
Public bytEndTime() As Byte
Public chrCardFalse As Char
Public bytCardReturn As Byte
Public bytCardPassWord() As Byte
Public bytFingerStart() As Byte
Public bytFingerEnd() As Byte
Public chrFingerFalse As Char
Public bytFingerReturn As Byte
Public bytFingerPW() As Byte
Public usNextPointer As Int16
Public usBeforePointer2 As Int16
Public usNextPointer2 As Int16
Public usBeforePointer3 As Int16
Public usNextPointer3 As Int16
Public Property ID()
Get
ID = bytUserID.Length - 1
End Get
Set(ByVal value)
ReDim bytUserID(value)
End Set
End Property
Public Property PW1()
Get
PW1 = bytCard1PW.Length - 1
End Get
Set(ByVal value)
ReDim bytCard1PW(value)
End Set
End Property
Public Property PW2()
Get
PW2 = bytCard2PW.Length - 1
End Get
Set(ByVal value)
ReDim bytCard2PW(value)
End Set
End Property
Public Property PW()
Get
PW = bytPassPower.Length - 1
End Get
Set(ByVal value)
ReDim bytPassPower(value)
End Set
End Property
Public Property S_Time()
Get
S_Time = bytStartTime.Length - 1
End Get
Set(ByVal value)
ReDim bytStartTime(value)
End Set
End Property
Public Property E_Time()
Get
E_Time = bytEndTime.Length - 1
End Get
Set(ByVal value)
ReDim bytEndTime(value)
End Set
End Property
Public Property CardPW()
Get
CardPW = bytCardPassWord.Length - 1
End Get
Set(ByVal value)
ReDim bytCardPassWord(value)
End Set
End Property
Public Property F_Start()
Get
F_Start = bytFingerStart.Length - 1
End Get
Set(ByVal value)
ReDim bytFingerStart(value)
End Set
End Property
Public Property F_End()
Get
F_End = bytFingerEnd.Length - 1
End Get
Set(ByVal value)
ReDim bytFingerEnd(value)
End Set
End Property
Public Property F_PassWord()
Get
F_PassWord = bytFingerPW.Length - 1
End Get
Set(ByVal value)
ReDim bytFingerPW(value)
End Set
End Property
End Structure
使用时:
Seek(fileNum, fileHead.intDataStart)
Dim U_Data As New UserData
'U_Data.F_ID = 15
'U_Data.F_Data = 577
U_Data.ID = 15
U_Data.PW1 = 15
U_Data.PW2 = 15
U_Data.PW = 15
U_Data.S_Time = 2
U_Data.E_Time = 2
U_Data.CardPW = 3
U_Data.F_Start = 2
U_Data.F_End = 2
U_Data.F_PassWord = 3
FileGet(fileNum, U_Data)
fileGet函数出错,错误代码5 ,请各位高手指教下。不胜感激!
[解决办法]
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Module App
Sub Main()
Serialize()
Deserialize()
End Sub
Sub Serialize()
' Create a hashtable of values that will eventually be serialized.
Dim addresses As New Hashtable
addresses.Add( "Jeff ", "123 Main Street, Redmond, WA 98052 ")
addresses.Add( "Fred ", "987 Pine Road, Phila., PA 19116 ")
addresses.Add( "Mary ", "PO Box 112233, Palo Alto, CA 94301 ")
' To serialize the hashtable (and its key/value pairs),
' you must first open a stream for writing.
' In this case, use a file stream.
Dim fs As New FileStream( "DataFile.dat ", FileMode.Create)
' Construct a BinaryFormatter and use it to serialize the data to the stream.
Dim formatter As New BinaryFormatter
Try
formatter.Serialize(fs, addresses)
Catch e As SerializationException
Console.WriteLine( "Failed to serialize. Reason: " & e.Message)
Throw
Finally
fs.Close()
End Try
End Sub
Sub Deserialize()
' Declare the hashtable reference.
Dim addresses As Hashtable = Nothing
' Open the file containing the data that you want to deserialize.
Dim fs As New FileStream( "DataFile.dat ", FileMode.Open)
Try
Dim formatter As New BinaryFormatter
' Deserialize the hashtable from the file and
' assign the reference to the local variable.
addresses = DirectCast(formatter.Deserialize(fs), Hashtable)
Catch e As SerializationException
Console.WriteLine( "Failed to deserialize. Reason: " & e.Message)
Throw
Finally
fs.Close()
End Try
' To prove that the table deserialized correctly,
' display the key/value pairs.
Dim de As DictionaryEntry
For Each de In addresses
Console.WriteLine( "{0} lives at {1}. ", de.Key, de.Value)
Next
End Sub
End Module