我是个初学者,想请问各位先生,如何读写随机文件?以下是郑阿奇先生著的<visual basic实用教程>中的例子:
Option Explicit
Public Score As student
Type student
     Number As String * 4
     Name As String * 10
     Chinesescore As Integer
End Type
Dim Length As Long, Num As Long
Dim Filenum As IntegerPrivate Sub Form_Load()
    Filenum = FreeFile
    Length = Len(Score)
    Num = 1
       '用随机方式打开
    Open "考试成绩" For Random As Filenum Len = Length
End SubPrivate Sub txtname_Change()
    Score.Name = txtName.Text
End SubPrivate Sub txtnumber_Change()
    Score.Number = txtNumber.Text
End SubPrivate Sub txtScore_Change()
    Score.Chinesescore = Val(txtScore.Text)
End Sub
Private Sub cmdInput_Click()
   '将文本筐内容写入文件中
    Dim i As Integer
    Num = LOF(Filenum) / Length + 1
    If txtNumber.Text = "" And txtName.Text = "" And txtScore.Text = "" Then
       MsgBox "无数据输入", "输入数据"
    Else
       Put #Filenum, Num, Score
       txtNumber.Text = ""
       txtName = ""
       txtScore.Text = ""
       Num = Num + 1
    End If
End Sub
Private Sub cmdDelete_Click()
  '根据输入的记录号删除记录
    Dim Filenum1 As Integer, Reanum As Long
    Dim Writenum As Long, Deletenum As Long
    Filenum1 = FreeFile
    Deletenum = Str(InputBox("请输入需删除的记录号"))
    If Deletenum <> 0 Then
       Open "考试成绩1" For Random As #Filenum Len = Length
       Seek #Filenum, 1
       '用seek函数指定记录号位置为1
       Do While Not EOF(Filenum) And readnum < LOF(Filenum) / Length
          readnum = readnum + 1
          Get #Filenum, readnum, Score
          If readnum <> Deletenum Then
          '与删除记录号进行比较,不是删除号就将该记录号写入临时文件中
             Writenum = Writenum + 1
             Put #Filenum1, Writenum, Score
          End If
          Close #Filenum
          Kill "考试成绩"
          Close #Filenum1
          Name "考试成绩1" As "考试成绩"
    End If
End Sub
Private Sub cmdEnd_Click()
    Close
    End
End Sub
我想在其中加入打开文件按纽,用来打开其中任何一个记录,怎么样来添加代码呢?因为我是个初手,故请能提供完整的例子和讲解好吗?谢谢!

解决方案 »

  1.   

    Private Sub Command1_Click()
          Dim FileName As String
          Dim StrArr() As String
          Dim TmpStr() As String
          Dim WritStr As String
          Dim I As Long
          
          FileName = "c:\lx1.txt"
          '将文件读到一个字符串数组.
          StrArr = RedTextFile(FileName)
          For I = 0 To UBound(StrArr)
              TmpStr = Split(StrArr, "=")
              '查找符合条件的项
              If UCase$(TmpStr(0)) = "ZZZ" Then
                 '修改该项
                 TmpStr(1) = "456"
                 StrArr(I) = TmpStr(0) & "=" & TmpStr(1)
              End If
              WritStr = WritStr & StrArr(I) & Chr(13)
          Next
          '回写文件
          WritTextFile FileName, WritStr      
    End Sub'读TEXT文件
    '函数:RedTextFile
    '参数:FileName 打开的TXT文件名.
    Public Function RedTextFile(FileName As String) As String()
         Dim FileID As Long
         Dim InputStr As String
         Dim LineStr As String
         Dim RevStr() As String
         Dim ID As Long
         
         On Error Resume Next
         
         InputStr = "": LineStr = ""
         FileID = FreeFile()
         Open FileName For Input As #FileID
              Do While Not EOF(FileID)           ' 循环至文件尾。
                 LineStr = ""
                 ID = ID + 1
                 ReDim Preserve RevStr(ID)
                 Line Input #FileID, LineStr
                 RevStr(ID - 1) = LineStr
              Loop
         Close #FileID
         RedTextFile = RevStr
         Err.Clear
    End Function'写TEXT文件
    'Private Sub Form_Load()
    '    Call WritTextFile("c:\111.txt", "ONE LINE" & vbCrLf & "TWO LINE")
    'End Sub
    '函数:WritTextFile
    '参数:FileName 目标文件名.WritStr 写到目标的字符串.
    '返回值:成功 返回文件内容.失败  返回""
    '注:如果同名,目标字符串将覆盖原文件内容.
    Public Function WritTextFile(FileName As String, WritStr As String) As Boolean
    '/保存文件
        Dim FileID As Long, ConTents As String
        Dim A As Long, B As Long
        
        On Error Resume Next
        
        FileID = FreeFile
        Open FileName For Output As #FileID
             Print #FileID, WritStr
        Close #FileID
        WritTextFile = (Err.Number = 0)
        Err.Clear
    End Function
      

  2.   

    读写文本我比较知道,我就是很不明白用type定义的随机文件的新建,打开,保存应用,因没人教全靠自己看书,请能给出这样的例子详细说明好吗?谢谢!
      

  3.   

    Option ExplicitFunction FsoRead(ByVal PFile As String) As String
        On Error GoTo Er
        Dim u As Object
        Dim X As TextStream
        Set u = CreateObject("scripting.filesystemobject")
        Set X = u.OpenTextFile(PFile, ForReading, True)
        FsoRead = X.ReadAll
        Exit Function
    Er:
        Dim y As FileSystemObject
        Set y = CreateObject("scripting.filesystemobject")
        Set u = y.CreateTextFile(PFile, True)
    End FunctionFunction FsoWrite(ByVal PFile As String, ByVal OutFile As String) As Boolean
        On Error GoTo Er
        Dim Ob As Object
        Dim X As TextStream
        Set Ob = CreateObject("scripting.filesystemobject")
        Set X = Ob.OpenTextFile(PFile, ForWriting, True)
        X.Write OutFile
        Exit Function
    Er:
        Dim y As FileSystemObject
        Set y = CreateObject("scripting.filesystemobject")
        Set Ob = y.CreateTextFile(PFile, True)
        X.Write OutFile
    End FunctionFunction FsoDel(ByVal PFile As String) As Boolean
        On Error GoTo Er
        Dim a As New FileSystemObject
        a.DeleteFile PFile, True
    Er:
    End Function