(VB6) Reading text files line by line looking for specific words

北慕城南 提交于 2019-12-06 07:44:30

Here's a function you could use to load the contents of a file:

Public Function LoadFile(dFile As String) As String

    Dim ff As Integer

    On Error Resume Next

    ff = FreeFile
    Open dFile For Binary As #ff
        LoadFile = Space(LOF(ff))
        Get #ff, , LoadFile
    Close #ff

End Function

Next, you want to split the output of that file. First, you will need to know what type of EOL termination character will be produced by the back-end. Assuming each line ends with a carriage return (13) and a line feed (10), you could use this code to store each line into a string array:

Dim lines() As String
lines = Split(LoadFile("LANrealm.log"), vbCrLf)

Finally, it's a matter of cycling through each line (using a For...Next loop) and look for whatever information you want to extract:

For i = 0 To Ubound(lines)
    ' Add here necessary logic to extract the information.
    ' Each line can be accessed by indexing the array as: lines(i)
Next

Hope this helps you get started...


To test the code:

  • Start VB6 and create a new project. VB6 will create an empty project with one form
  • Double click the form to view it
  • Right click the Toolbox and select "Components"
  • Locate the "Microsoft Common Dialog Control" and select it
  • Click OK
  • Now, drag the "CommonDialog" component from the Toolbox onto the form
  • Double click the form to view its source code
  • Paste the following code

NOTE: Make sure you overwrite any pre-existing code

Option Explicit

Private Sub Form_Load()
    cDlg.DefaultExt = "txt"
    cDlg.Filter = "Text Files|*.txt;*.log"
    cDlg.ShowOpen

    If cDlg.fileName <> "" Then AnalyzeFile .fileName
End Sub

Private Sub AnalyzeFile(fileName As String)
    Dim fileContents As String
    Dim lines() As String
    Dim i As Integer

    fileContents = LoadFile(fileName)
    lines = Split(fileContents, vbCrLf)

    For i = 0 To UBound(lines)
        If InStr(1, lines(i), "event:", vbTextCompare) Then
            MsgBox "Line #" & i & " contains the string 'event'" + vbCrLf + vbCrLf + lines(i)
        End If
    Next
End Sub

Private Function LoadFile(dFile As String) As String
    Dim ff As Integer
    On Error Resume Next

    ff = FreeFile
    Open dFile For Binary As #ff
        LoadFile = Space(LOF(ff))
        Get #ff, , LoadFile
    Close #ff
End Function

Run the program and, when asked to supply a file, select one of the logs that will be generated by the back-end.

In this example, the program will tell you which lines contain "event information", such as "Event: Roper killed Pleb (M4A1) shots=5 Feet=2 Body=2 Head=1".

One problem I would see doing this in real-time, reading and writing is if two or more computers or apps try to open the same file. This could be a bit of a mess.

Ok, so if you REALLY want to read your file line by line, I would change your logfile a little different.

******************
LANrealm Match Log
******************

Game:  Call of Duty 4
Game Type:  Team Deathmatch
Date: 01-Jan-2013
Time:1 9:00:00
Players: Tramp, Roper, d00b, Pleb
Score Limit:    150

Event:  Game Start  
Event:  Roper killed Pleb (M4A1) shots=5 Feet=2 Body=2 Head=1 
Event:  Tramp committed suicide (Fall damage)
Event:  Tramp killed d00b (Grenade)
Event:  Pleb said "I'm saying something"
Event:  Pleb teamkilled d00b (G3) shots=3 Feet=0 Body=2 Head=1 
Event:  Game Finished

Winner: Roper

Stat: Roper Kills=1,Deaths=0,Suicides=0,Teamkills=0
Stat: Tramp Kills=1,Deaths=0,Suicides=1,Teamkills=0
Stat: Pleb Kills=0,Deaths=0,Suicides=0,Teamkills=1
Stat: d00b Kills=0,Deaths=0,Suicides=0,Teamkills=0

You can use this to read the file line by line.

   Dim FileNo As Integer
   Dim TempData As String
   Dim TempStr As String
   FileNo = FreeFile
   Open "c:\game.log" For Input As FileNo
      Do
         Line Input #FileNo, TempStr
         TempData = TempData & TempStr or do what ever you want it to do with that line.
         DoEvents
      Loop Until EOF(FileNo)
   Close #FileNo
   MsgBox TempData

However, I would suggest to read the whole file into a string, and then parse out info you want. If you did, you can then pick out info you might want, like name of game etc.....

Try this. Create a new module, and paste this in it.

Public Function ParseData(DataSTR As String, StartSTR As String, EndSTR As String) As String
   Dim Split1
   Dim Split2
   Split1 = Split(DataSTR, StartSTR, , 1)
   Split2 = Split(Split1(1), EndSTR, , 1)
   SplitParse = Split2(0)
End Function

Then add this to a command button.

Private Sub Command2_Click()
 Dim FileNo As Integer
   Dim TempData As String
   FileNo = FreeFile
   Open "c:\game.log" For Input As FileNo
      TempData = Input(LOF(FileNo), FileNo)
   Close
   MsgBox TempData
   MsgBox Trim(ParseData(TempData, "Game:", Chr(10)))
End Sub

Now this is just a sample of what you can do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!