请问,vb能不能对一个文件同时读写,我现在想要在一个文件中查找一个字段,如果找不到就写入这个字段,不知道该如何实现?

解决方案 »

  1.   

    可以!
    若是字符串,需先把待查串用strconv转成ANSI串的Byte数组,用Binary方式打开文件,读入Byte数组,用instrB查找。若文件很大,还要做分段读取处理。
      

  2.   

    homezj(小吉)  ,谢谢你,我很少用vb,能不能麻烦你给一个例子呀,我着急用,谢谢了
      

  3.   

    Option Explicit
    Private Const SplitStr1 As String = "("
    Private Const SplitStr2 As String = ")"
    Private Function SerchFile(File As String, SerchByte() As Byte) As Boolean
        Dim FreeNo As Long, a() As Byte, FindOk As Boolean, Length As Long
        FreeNo = FreeFile
        Open File For Binary As #FreeNo
        Length = LOF(FreeNo)
        If Length > 0 Then
            ReDim a(Length - 1)
            Get #FreeNo, , a
            If InStrB(1, a, SerchByte) > 0 Then FindOk = True
        End If
        If FindOk = False Then Put #FreeNo, Length + 1, SerchByte
        Close #FreeNo
        SerchFile = FindOk
    End Function
    Private Sub Command1_Click()
        Dim a() As Byte
        a = StrConv(SplitStr1 & Text1.Text & SplitStr2, vbFromUnicode)
        If SerchFile("c:\1.txt", a) Then
            MsgBox "关键字存在"
        Else
            MsgBox "关键字不存在,已添加到文件尾部"
        End If
    End Sub
      

  4.   

    多谢了 homezj(小吉) ,我试试看