回冈 Label ex tBox Name TextBox2 月薪 TextBox 写文件读文件 图64输入界面 编写程序代码如下 定义 Person结构及其全局变量 Employee Structure person Public ID As Integer Public Monthly salary As Decimal <VBFixedString( 8)> Public Name As String End structure Public Employee as person Public position As Long =0 记录位置变量 写入文件
编写程序代码如下: ' 定义Person结构及其全局变量Employee Structure Person Public ID As Integer Public MonthlySalary As Decimal <VBFixedString(8)> Public Name As String End Structure Public Employee As Person Public Position As Long = 0 ‘记录位置变量 ‘写入文件
Private Sub Button1_Click (ByVal sender As System Object, ByVal e as System EventArgs) Handles Button 1. Click Dim Reclength As Long Position + 1 Reclength Len(employee. Monthly Salary) RecLength Len(employee) FileOpen (1, "MYFILE. daT 4, Reclength) Employee ID= Val(TextBox1Text Employee. Name= TextBox2 Text Employee. MonthlySalary =Val(TextBoX3 Text) FilePut(1, Employee, Position) FileClose(1) End sub 从文件读出 Private Sub Button2_Click (By Val sender As system Object, ByVal e As System. EventArgs)Handles Button2. Click
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim RecLength As Long Position += 1 RecLength = Len(Employee.MonthlySalary) RecLength = Len(Employee) FileOpen(1, "MYFILE.DAT", 4, , , RecLength) Employee.ID = Val(TextBox1.Text) Employee.Name = TextBox2.Text Employee.MonthlySalary = Val(TextBox3.Text) FilePut(1, Employee, Position) FileClose(1) End Sub ‘从文件读出 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim FileNum As Integer, i, sum Reclength As Long Reclength Len(employee) FileNum = FreeFileo FileOpen(FileNum,"MYFILE. DaT OpenMode Random, Reclength) sum= LOF (FileNum)/ Reclength For i= 1 To sum File Get(FileNum, Employee, D Label4. Text & Employee ID Employee Name Employee. MonthlySalary chr(10) Next File Close(FileNum) End sub 程序运行的结果如图65乐示
Dim FileNum As Integer, i, sum, RecLength As Long RecLength = Len(Employee) FileNum = FreeFile() FileOpen(FileNum, "MYFILE.DAT", OpenMode.Random, , , RecLength) sum = LOF(FileNum) / RecLength For i = 1 To sum FileGet(FileNum, Employee, i) Label4.Text &= Employee.ID & Employee.Name & Employee.MonthlySalary & Chr(10) Next FileClose(FileNum) End Sub 程序运行的结果如图6-5所示
Ea Foral 回区 2002001sun 8000 D 2002001 1000rext12345 Nan 月薪 写文件读文件 图6-5运行结果4 4二进制文件 二进制文件是一种以二进制方式编码和保存数据的文件, 对于以 Binary(二进制)模式打开的文件,大多数 Random模式规则都 适用,只有少数例外。如: FileOpen函数中的 RecordLength子句没有任何作用。 Fileput连续向磁 盘中写入所有变量,即:记录之间没有空白 对于结构中数组以外的任何数组, FilePut都只写入数据。未写入任何说 明符
4 二进制文件 二进制文件是一种以二进制方式编码和保存数据的文件, 对于以Binary(二进制)模式打开的文件,大多数 Random 模式规则都 适用,只有少数例外。如: FileOpen 函数中的 RecordLength 子句没有任何作用。FilePut 连续向磁 盘中写入所有变量,即:记录之间没有空白。 对于结构中数组以外的任何数组,FilePut 都只写入数据。未写入任何说 明符
u写 写入的字节数等子学符串中的符数例口下面的语可将个学节写人1 号文 Dim hello As string ="Hello World FilePut(1, hello) 下例使用 Fileput函数将3条结构 Person的记录被写入文件 Structure person Public Id As integer Public Name As String End structure Sub WriteDatao Dim PatientRecord As person Dim recordNumber As Integer FileOpen(1,"TESTFILE. txt",OpenMode Binary) 以 Binary 模式打并的文 For recordnumber= 1 to 3 循环5次 PatientRecord, id= recordNumber 定义ID PatientRecord. Name="Name"& recordNumber'定义字符串
FilePut 写入不是结构元素且不带两个字节的长度说明符的变长字符串。 写入的字节数等于字符串中的字符数。例如,下面的语句将11个字节写入1 号文件。 Dim hello As String = "Hello World" FilePut(1,hello) 下例使用 FilePut 函数将3条结构 Person 的记录被写入文件: Structure Person Public ID As Integer Public Name As String End Structure Sub WriteData() Dim PatientRecord As Person Dim recordNumber As Integer FileOpen(1, " TESTFILE.txt", OpenMode.Binary) '以Binary 模式打开的文件 For recordNumber = 1 To 3 '循环5次 PatientRecord.ID = recordNumber '定义ID PatientRecord.Name = "Name " & recordNumber '定义字符串