怎样在VB中把结构体数组保存到二进制文件中?
怎样在VB中把结构体数组保存到二进制文件中?并且再把二进制文件中的内容恢复到结构体数组中。如果结构中有byte数组,应该怎样存取?...
怎样在VB中把结构体数组保存到二进制文件中?并且再把二进制文件中的内容恢复到结构体数组中。
如果结构中有byte数组,应该怎样存取? 展开
如果结构中有byte数组,应该怎样存取? 展开
1个回答
展开全部
Option Explicit
'定义结构体,注意,此处如果用到字符串变量,要设置定长
Private Type typUser
uid As Long
uName As String * 10 '定长10个字符
uPWD As String * 10
End Type
Dim user(10) As typUser
'读取
Dim userGet(10) As typUser
Private Sub Form_Load()
'随便填充数据
Dim i As Integer
For i = 0 To 10
With user(i)
.uid = i
.uName = Rnd * 99999
.uPWD = Rnd * 88888
End With
Next
'保存结构体数组到文件
Open App.Path & "\save.dat" For Random As #1 Len = Len(user(0)) 'Random方式打开文件,这里的len是每条记录的长度
For i = 0 To 10
Put #1, , user(i)
Next
Close #1
'从文件中读取结构体
Open App.Path & "\save.dat" For Random As #1 Len = Len(user(0))
For i = 0 To 10
Get #1, i + 1, userGet(i)
Next
Close #1
End Sub
'定义结构体,注意,此处如果用到字符串变量,要设置定长
Private Type typUser
uid As Long
uName As String * 10 '定长10个字符
uPWD As String * 10
End Type
Dim user(10) As typUser
'读取
Dim userGet(10) As typUser
Private Sub Form_Load()
'随便填充数据
Dim i As Integer
For i = 0 To 10
With user(i)
.uid = i
.uName = Rnd * 99999
.uPWD = Rnd * 88888
End With
Next
'保存结构体数组到文件
Open App.Path & "\save.dat" For Random As #1 Len = Len(user(0)) 'Random方式打开文件,这里的len是每条记录的长度
For i = 0 To 10
Put #1, , user(i)
Next
Close #1
'从文件中读取结构体
Open App.Path & "\save.dat" For Random As #1 Len = Len(user(0))
For i = 0 To 10
Get #1, i + 1, userGet(i)
Next
Close #1
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询