如何使用 vb 写入文本文件中的特定行

发布于 2024-10-27 15:59:33 字数 523 浏览 2 评论 0原文

我有一个包含此示例数据的文本文件

绑架测试|1
芯片测试|2
帽子测试|3
邪恶测试|4

我想知道如何循环它以找到用户并删除该行。

这是我到目前为止所拥有的:

Public Sub RemoveMember(member As String)
Dim u As String, strdata() As String

Open (App.Path & "\Membership.txt") For input As #1
    Do

    input #1, u

    strdata = Split(u, "|")

    If strdata(0) = member Then
        'figure out a way to remove this line from the text file'
    End If

    Loop Until EOF(1)

Close #1
End Sub

I have a text file with this sample data

abduct test|1
chip test|2
hatter test|3
evil test|4

I would like to know how I could loop through it to find a user and remove that line.

This is what I have so far:

Public Sub RemoveMember(member As String)
Dim u As String, strdata() As String

Open (App.Path & "\Membership.txt") For input As #1
    Do

    input #1, u

    strdata = Split(u, "|")

    If strdata(0) = member Then
        'figure out a way to remove this line from the text file'
    End If

    Loop Until EOF(1)

Close #1
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2024-11-03 15:59:33

我会说:

  • 逐行读入
  • 检查该行是否包含要删除的成员
  • 如果不包含则将该行写回新文件 读取
  • 所有行后删除原始文件
  • 将新文件重命名为原始文件

Stefan 的方法可能更快,但如果文件变得非常大,则会使用大量内存。

我不熟悉VB的本地文件方法。使用 FileSystemObject(参考 Microsoft Scripting Host),您将得到:

Dim clsOriginalFile as TextStream
Dim clsNewFile as TextStream
Dim FSO as New FileSystemObject
Dim varLine as Variant
Dim strLine as String

set clsOriginalFile=FSO.OpenTextFile "members.txt", ForReading
set clsNewFile =FSO.OpentTextFile "temp.txt", ForWriting, True

Do While Not clsOriginalFile.AtEndOfStream
    varLine = clsOriginalFile.ReadLine
    strLine=varLine
    If instr(strLine,member)=0 Then
        clsNewFile.WriteLine strLine
    End If
Loop

clsOriginalFile.Close
clsNewFile.Close

FSO.DeleteFile("members.txt")
FSO.MoveFile("temp.txt","members.txt")

无需 IDE 的帮助即可编写,因此代码中可能存在一些拼写错误。

I would say:

  • Read in the lines one by one
  • Check if the line contains the member to be deleted
  • Write back the line to a new file if it does not
  • After reading all the lines delete the original file
  • Rename the new file to the original file

Stefan's method is probably faster but will use a lot of memory if the file grows very large.

I am not familiar wityh VB's native file method. Using FileSystemObject (reference Microsoft Scripting Host) you wil get:

Dim clsOriginalFile as TextStream
Dim clsNewFile as TextStream
Dim FSO as New FileSystemObject
Dim varLine as Variant
Dim strLine as String

set clsOriginalFile=FSO.OpenTextFile "members.txt", ForReading
set clsNewFile =FSO.OpentTextFile "temp.txt", ForWriting, True

Do While Not clsOriginalFile.AtEndOfStream
    varLine = clsOriginalFile.ReadLine
    strLine=varLine
    If instr(strLine,member)=0 Then
        clsNewFile.WriteLine strLine
    End If
Loop

clsOriginalFile.Close
clsNewFile.Close

FSO.DeleteFile("members.txt")
FSO.MoveFile("temp.txt","members.txt")

Written without the help of the IDE, so there may a few typos in the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文