VB怎样读写有文件头信息的二进制文件
'1、我用自定义的变量只能读写没有文件头的二进制文件?
'2、用Random模式打开读取记录总是从第一个字节开始读文件不能指定从某个位置开始读取?
'3、写入的二进制文件的字段间隔标识是二个字节的,怎样才能使用一个字节的间隔标识?
Public Type WJTXX
版本 As String * 18
密码 As String * 18
标识 As Boolean
定长 As Integer
End Type
Public Type ZDYZFC
bm As String
mc As String
dw As String
jg As Double
xs As Double
bz As String
End Type
Dim WJNL As WJTXX
Dim SJCL As ZDYZFC
Private Sub Command1_Click()
WJNL.版本="V1.0"
WJNL.密码="123456"
WJNL.标识= True
WJNL.定长= 200
Open App.Path & "\.bat" For Random As #1 Len = 300 ' Random模式打开
Put #1, , WJNL
Seek #1, 201
For i = 1 To MSFlexGrid1.Rows - 1
SJCL.bm = MSFlexGrid1.TextMatrix(i, 1)
SJCL.mc = MSFlexGrid1.TextMatrix(i, 2)
SJCL.dw = MSFlexGrid1.TextMatrix(i, 3)
SJCL.jg = Val(MSFlexGrid1.TextMatrix(i, 4))
SJCL.xs = Val(MSFlexGrid1.TextMatrix(i, 5))
SJCL.bz = Mid(MSFlexGrid1.TextMatrix(i, 6))
Put #1, i, SJCL
Next
Close #1
End Sub
Private Sub Command2_Click()
Open App.Path & "\.bat" For Random As #1 Len = 300 ' Random模式打开
Get #1, , WJNL
Text1= WJNL.版本
Text2= WJNL.密码
Text3= WJNL.标识
Text4= WJNL.定长
i = 1
Do While Not EOF(1)
Seek #1, 201
Get #1, i, SJCL
MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
MSFlexGrid1.TextMatrix(i, 0) = i
MSFlexGrid1.TextMatrix(i, 1) = Trim(SJCL.bm)
MSFlexGrid1.TextMatrix(i, 2) = Trim(SJCL.mc)
MSFlexGrid1.TextMatrix(i, 3) = Trim(SJCL.dw)
MSFlexGrid1.TextMatrix(i, 4) = IIf(SJCL.jg <> 0, SJCL.jg, "")
MSFlexGrid1.TextMatrix(i, 5) = IIf(SJCL.xs <> 0, SJCL.xs, "")
MSFlexGrid1.TextMatrix(i, 6) = Trim(SJCL.bz)
i = i + 1
Loop
Close #1
End Sub
[解决办法]
比如文件头有12个字节,则:
dim FileHeader(11) as byte
open "XXX.bin" for binary as #1
get #1,,fileheader()
close #1
这fileheader()里的12个字节就是了.