返回首页
VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
  • vb.net教程之随机文件的读写操作

  • 2015-01-18 15:12 来源:未知
随机文件的读写操作(相关vb.net教程)
随机文件的打开也用fileopen完成,只是打开随机文件的形式为:openmode.random
和二进制文件的操作一样,读随机文件用的是fileget方法,写入随机文件用的是fileput方法。
应用实例:将一批个人记录存放到随机文件中。[实验报告41 ]
见,随机文件的读写个人记录的存取,程序为:
Public Class Form1
    Structure person
        Dim name As String
        Dim age As Integer
        Dim sex As String
        Dim note As String
    End Structure
    Dim filename As String = "d:\p.txt"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fn As Integer
        Dim p As person
        fn = FreeFile()
        FileOpen(fn, filename, OpenMode.Random)
        p.name = "黄子昱" : p.age = 15 : p.sex = "男"
        p.note = "中学生" : FilePut(fn, p)
        p.name = "李立立" : p.age = 10 : p.sex = "男"
        p.note = "小学生" : FilePut(fn, p)
        p.name = "王安石" : p.age = 5 : p.sex = "男"
        p.note = "儿 童" : FilePut(fn, p)
        FileClose()
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim fn As Integer
        Dim p As person
        Dim s As String = ""
        fn = FreeFile()
        FileOpen(fn, filename, OpenMode.Random)
        Do While Not EOF(fn)
            FileGet(fn, p)
            s = s + p.name + "  " + p.sex + "  " + Str(p.age) + "  " + p.note + vbCrLf
        Loop
        FileClose()
        TextBox1.Text = s
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        End
    End Sub
End Class
说明:
记录对文件的读写操作是整体进行的(即不分域,而是整个记录读、写),但要给记录赋值,却要就记录的每个域分别进行的。
综合实例:设计一个人员记录录入窗口,可以完成记录的增加、删除、保存操作。同时还可以进行记录的前后浏览:第一条记录,下一条,前一条,最后一条记录。程序见,人员记录的录入与浏览--文件操作综合。[实验报告42]
此时在当前目录下就生成一个数据文件:
L:\vb.net\人员记录的录入与浏览--文件操作综合\人员记录的录入与浏览--文件操作综合\bin\Debug\person.dat
程序为:
Imports System.IO
Public Class Form1
    Structure personstruct
        Dim name, sex, tel As String
        Dim age As Integer
    End Structure
    Dim datafile As String
    Dim person() As personstruct
    Dim curp As Integer
    Private Sub readdata()
        '从文件中读取记录数据到数组person中
        Dim n, fn As Integer
        Dim ps As New personstruct
        fn = FreeFile()
        FileOpen(fn, datafile, OpenMode.Random)
        n = 0
        Do While Not EOF(fn)
            FileGet(fn, ps)
            ReDim Preserve person(n)
            person(n) = ps
            n = n + 1
        Loop
        FileClose(fn)
    End Sub
    Private Sub savedata()
        '把数组person中的数据保存到文件中
        Dim n, fn As Integer
        If File.Exists(datafile) Then File.Delete(datafile)
        If IsNothing(person) Then Exit Sub
        fn = FreeFile()
        FileOpen(fn, datafile, OpenMode.Random)
        For n = 0 To UBound(person)
            FilePut(fn, person(n))
        Next
        FileClose(fn)
    End Sub
    Private Sub getdata()
        '将数组中记录显示在窗口中
        Dim flag As Boolean = False
        If Not IsNothing(person) Then
            If (curp >= 0 And curp <= UBound(person)) Then
                Textname.Text = person(curp).name
                Textage.Text = Trim(Str(person(curp).age))
                Texttel.Text = person(curp).tel
                male.Checked = IIf(person(curp).sex = "男", True, False)
                female.Checked = IIf(person(curp).sex = "男", False, True)
                flag = True
                gbdata.Visible = True
                lbpos.Text = "当前记录:" + Str(curp + 1) + "/" + Str(UBound(person) + 1)
            End If
        End If
        If flag = False Then
            gbdata.Visible = False
            lbpos.Text = ""
        End If
    End Sub
    Private Sub putdata()
        '把当前控件中的数据保存到数组person中
        If Not IsNothing(person) Then
            If (curp >= 0 And curp <= UBound(person)) Then
                person(curp).name = Textname.Text
                person(curp).sex = IIf(male.Checked, "男", "女")
                person(curp).age = Val(Textage.Text)
                person(curp).tel = Texttel.Text
            End If
        End If
    End Sub
 
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        '程序关闭时保存各个记录数据
        savedata()
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '程序启动时确定数据文件datafile在当前目录中
        datafile = Application.StartupPath
        If Microsoft.VisualBasic.Right(datafile, 1) <> "\" Then
            datafile = datafile + "\"
        End If
        datafile = datafile + "person.dat"
        '读文件数据
        readdata()
        '显示记录
        curp = 0
        getdata()
    End Sub
 
    Private Sub add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add.Click
        '增加记录
        Dim n As Integer
        If IsNothing(person) Then
            n = 0
        Else
            n = UBound(person) + 1
        End If
        ReDim Preserve person(n)
        person(n).name = ""
        person(n).sex = "男"
        person(n).age = 0
        person(n).tel = ""
        '显示新增加的空记录
        curp = n
        getdata()
    End Sub
 
    Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
        '保存数据到person数组中
        putdata()
    End Sub
 
    Private Sub first_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles first.Click
        '转到第一条记录
        If Not IsNothing(person) Then
            curp = 0
            getdata()
        End If
    End Sub
 
    Private Sub prior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles prior.Click
        '转到前一条记录
        If Not IsNothing(person) Then
            If curp > 0 Then curp = curp - 1
            getdata()
        End If
    End Sub
 
    Private Sub thenext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles thenext.Click
        '转到下一条记录
        If Not IsNothing(person) Then
            If curp < UBound(person) Then curp = curp + 1
            getdata()
        End If
    End Sub
 
    Private Sub last_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles last.Click
        '转到最后一条记录
        If Not IsNothing(person) Then
            curp = UBound(person)
            getdata()
        End If
    End Sub
 
    Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
        '删除当前记录
        Dim i, n As Integer
        Dim ps As New personstruct
        If Not IsNothing(person) Then
            If MsgBox("确定要删除该记录?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "删除") = MsgBoxResult.Yes Then
                n = UBound(person)
                '把curp+1,purp+2...,n记录向前移动
                For i = curp + 1 To n
                    person(i - 1) = person(i)
                Next
                n = n - 1
                If n >= 0 Then
                    '调整person数组,删除它的最后一个元素
                    ReDim Preserve person(n)
                    If curp = n + 1 Then curp = n
                Else
                    '记录删除完毕
                    person = Nothing
                    curp = -1
                End If
                '显示新的记录
                getdata()
            End If
        End If
    End Sub
End Class
相关教程
返回首页