struct
STR
{
int
i
char
sz[8]
}
//定义一个变量
struct
STR
str
str.i
=
123
如果用C语言的方法,可以这样
FILE
pf
=
fopen("str.dat","wb")//注意,要以二进制方式打开文件
fwrite(&str,sizeof(str),1,pf)
读取的时候是一样的
fread(&str,sizeof(str),1,pf)
那个1表示一个结构体,如果你定义一个结构体数组,把1改成数组中元素个数就可以了
OptionExplicit
'定义结构体,注意,此处如果用到字符串变量,要设置定长
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)