(VB6) 逐行读取文本文件以查找特定单词 [英] (VB6) Reading text files line by line looking for specific words

查看:32
本文介绍了(VB6) 逐行读取文本文件以查找特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,StackOverflow,

Good afternoon StackOverflow,

我刚刚在这里注册 - 我已经使用这个网站多年了,它似乎总是提供答案的网站,所以我决定参与其中.

I've just signed up here - I've been using this site for ages, and it seems to always be the site to supply the answer so I decided to be part of things.

不用多说,这是我的问题 -

Without further ado, here is my question -

我正在为 LAN 派对编写 API,我和我的团队每月都会使用它来帮助解决记分方面的问题.我的朋友正在为它编写后端,而我正在编写 VB6 前端.自从我写 VB6 已经有一段时间了,我从来没有像我在这里瞄准的前端级别那样密集地写过它.

I am writing an API for LAN parties that me and a group have monthly to help sort things out on the scorekeeping side. My friend is writing the backend for it, and I'm writing the VB6 frontend. It's been a while since I wrote VB6, and I never wrote it as intensively as the grade of frontend I'm aiming for here.

该程序的前提是 - 后端将我们正在玩的游戏中的事件实时写入文本文件 - 前端实时读取.我现在想咨询的部分是这个 -

The premise of the program is this - The backend will write events from the game we're playing to a text file in realtime - Which the frontend reads from in realtime. The part I'd like to enquire about at the moment is this -

我知道您可以在 VB6 中逐行读取文本文件.我希望程序聆听"(可以这么说)某些流行语并使用它们定义的值"来影响变量.这是它将读取的文件类型的模拟示例 -

I know you can read text files line-by-line in VB6. I want the program to 'listen' (so to speak) for certain buzzwords and use their defined 'Values' to affect variables. Here is a mock example of the kind of file it'll be reading -

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

Game:       Call of Duty 4
Game Type:  Team Deathmatch
Date:       01-Jan-2013
Time:       19: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

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

嗯,我想只要看看这个,你就可以知道我希望程序从中挑选出什么.如果我只是让它完全用逗号分隔会容易得多,但我想保持原始文本文件的可读性.但是,是的,以防万一你没有得到它,我希望程序能够识别Roper"有 1 个Kill"等等.一个示例代码片段会很棒!

Well, I think just by looking at this you can tell what I want the program to pick out of that. It would be a lot easier if I just made it fully comma delimited, but I want to maintain readability of the raw text file. But yeah, just in case you didn't get it, I'd want the program to recognise that 'Roper' had 1 'Kill' and so on and so forth. An example code snippet would be great!

提前致谢,伙计们.

推荐答案

这是一个可以用来加载文件内容的函数:

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

接下来,您要拆分该文件的输出.首先,您需要知道后端将生成什么类型​​的 EOL 终止字符.假设每一行都以回车符 (13) 和换行符 (10) 结束,您可以使用此代码将每一行存储到一个字符串数组中:

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)

最后,循环遍历每一行(使用 For...Next 循环)并查找您想要提取的任何信息:

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...

测试代码:

  • 启动 VB6 并创建一个新项目.VB6 会用一种形式创建一个空项目
  • 双击表单进行查看
  • 右键单击工具箱并选择组件"
  • 找到Microsoft 通用对话框控件"并选择它
  • 点击确定
  • 现在,将CommonDialog"组件从工具箱拖到表单上
  • 双击表单查看其源代码
  • 粘贴以下代码

注意:确保覆盖任何预先存在的代码

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.

在这个例子中,程序会告诉你哪些行包含事件信息",例如事件:罗珀杀死平民(M4A1)射击=5英尺=2身体=2头部=1".

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".

这篇关于(VB6) 逐行读取文本文件以查找特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆