新建From1(窗体),新建Command1(按钮CommandButton),代码:
Private Sub Command1_Click()
Dim FileName As String
Dim SearchText As String
FileName = App.Path & "\manongku.txt"
SearchText = "库" '得到3
ret = FileContains(FileName, SearchText)
MsgBox ret
End Sub
Function FileContains(FileName As String, SearchText As String) As Long
Dim FileNumber As Integer
Dim FileLength As Long
Dim Chunk As String
Dim ChunkStart As Long
Dim FoundAt As Long
Const MaxChunk = 20000
On Error GoTo FileContainsError
FileNumber = FreeFile
Open FileName For Binary Access Read Shared As FileNumber
FileLength = LOF(FileNumber)
ChunkStart = 0
Do Until ChunkStart = FileLength
If FileLength - ChunkStart > MaxChunk Then
Chunk = Input$(MaxChunk, FileNumber)
ChunkStart = ChunkStart + MaxChunk - Len(SearchText)
Else
Chunk = Input$(FileLength - ChunkStart, FileNumber)
ChunkStart = FileLength
End If
FoundAt = InStr(Chunk, SearchText)
If FoundAt > 0 Then
FileContains = FoundAt
Exit Do
End If
Loop
Close FileNumber
Exit Function
FileContainsError:
Select Case Err
Case Else
MsgBox Error$ & " on file " & FileName
End Select
Exit Function
End Function
manongku.txt的内容:
码农库
MaNongKu.com
东西好呀,
真的好!