<address id="xpjh9"><listing id="xpjh9"><meter id="xpjh9"></meter></listing></address>

<address id="xpjh9"><address id="xpjh9"><listing id="xpjh9"></listing></address></address>
<noframes id="xpjh9">
<noframes id="xpjh9">

<address id="xpjh9"><address id="xpjh9"><listing id="xpjh9"></listing></address></address>
    <form id="xpjh9"></form><address id="xpjh9"><listing id="xpjh9"><menuitem id="xpjh9"></menuitem></listing></address>

      <noframes id="xpjh9">
      VB.net 2010 視頻教程 VB.net 2010 視頻教程 VB.net 2010 視頻教程
      SQL Server 2008 視頻教程 c#入門經典教程 Visual Basic從門到精通視頻教程
      當前位置:
      首頁 > 編程開發 > .net教程 >
      • 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
      相關教程
      江苏快3 马鞍山 | 吴忠 | 延边 | 山南 | 海东 | 萍乡 | 海安 | 海南海口 | 九江 | 陕西西安 | 永新 | 库尔勒 | 博尔塔拉 | 平凉 | 三门峡 | 项城 | 辽阳 | 安岳 | 大庆 | 普洱 | 大同 | 青州 | 广汉 | 大庆 | 长兴 | 株洲 | 包头 | 临沧 | 白山 | 安徽合肥 | 福建福州 | 南安 | 蚌埠 | 安庆 | 新疆乌鲁木齐 | 吕梁 | 景德镇 | 珠海 | 邹平 | 镇江 | 改则 | 乌兰察布 | 来宾 | 灌云 | 巴音郭楞 | 包头 | 大理 | 和田 | 昭通 | 攀枝花 | 黄山 | 南通 | 来宾 | 禹州 | 神农架 | 唐山 | 湛江 | 通化 | 万宁 | 江门 | 盐城 | 和县 | 安吉 | 果洛 | 泸州 | 克拉玛依 | 邵阳 | 许昌 | 霍邱 | 文昌 | 阿拉尔 | 昭通 | 潮州 | 阳泉 | 湖南长沙 | 亳州 | 张家界 | 朔州 | 通化 | 阿拉尔 | 甘南 | 改则 | 仁怀 | 伊春 | 瑞安 | 玉树 | 郴州 | 连云港 | 阿拉尔 | 曲靖 | 眉山 | 赤峰 | 武安 | 青州 | 武安 | 锦州 | 吉林长春 | 丽江 | 邹城 | 芜湖 | 仙桃 | 肥城 | 永州 | 海安 | 海门 | 新泰 | 吉林 | 宜昌 | 保山 | 日土 | 诸暨 | 北海 | 怒江 |