如何在visual basic中的文本文件中向特定行添加文本?​​

时间:2017-04-12 11:59:31

标签: vb.net vb.net-2010

我正在尝试使用Visual Studio 2010或2017在家中创建一个关卡项目的程序,并且正在使用控制台应用程序。我需要做的是创建一个文本文件,其中包含教师输入程序的学生详细信息。然后,教师需要能够输入特定的学生ID号,并能够在该学生的记录末尾添加分数。 到目前为止,这是我的代码并且可以正常工作。但它只允许教师输入学生的详细信息并将其写入文本文件。

    Dim studentID As String
    Dim studentname As String
    Dim classname As String

    Dim Filename As String
    Filename = “Students.txt”
    FileOpen(1, Filename, OpenMode.Append)

    Console.WriteLine("                                  Student Setup")
    Console.WriteLine()
    Console.WriteLine()

    Console.Write("Student ID:  ")
    studentID = Console.ReadLine
    Console.Write("Student Name:  ")
    studentname = Console.ReadLine
    Console.WriteLine()
    Console.Write("Class  :  ")
    classname = Console.ReadLine
    WriteLine(1, studentID, studentname, classname)

    Console.WriteLine("Student successfully setup.")

现在我希望能够让教师输入学生ID号和分数,然后将分数写在文本文件中以学生ID号开头的特定行的末尾。

文本文件的示例如下所示:

“12345”, “亚当”, “A”

“67534”, “爱丽丝”, “A”

“21456”, “鲍勃”, “A”

“98765”, “查尔斯”, “A”

“87654”, “丹尼斯”, “A”

然后,如果教师输入21456作为要添加分数的学生ID号,并且分数为6则则将数字6添加到该行的末尾,以便现在看到文本文件的行像这样:

“21456”, “鲍勃”, “A”, “6”

我尝试使用以下代码执行此操作,我从另一个论坛获取并尝试更改以执行我想要执行的操作:

    Dim score As Integer
    Dim studentIDenter As String
    Dim count As Integer

    Console.Write("Student ID to add score to: ")
    studentIDenter = Console.ReadLine
    Console.Write("Student score: ")
    score = Console.readline

    Dim lines() As String = System.IO.File.ReadAllLines(Filename)
    For count = 0 To lines.Length - 1
        If lines(count).StartsWith(studentIDenter) Then
            lines(count) = studentID & studentname & classname & score
            System.IO.File.WriteAllLines(Filename, lines)
        End If
    Next

    FileClose(1)

2 个答案:

答案 0 :(得分:0)

我假设用户在学生编号和分数周围输入引号。

您需要在支票中添加引号,以便在该行的开头添加匹配项。此外,当您将分数添加到行尾时,您需要在分数周围添加逗号和引号。

更改为:

For count = 0 To lines.Length - 1
    If lines(count).StartsWith(Chr(34) & studentIDenter & Chr(34)) Then
        lines(count) = lines(count) & "," & Chr(34) & score & Chr(34)
        System.IO.File.WriteAllLines(Filename, lines)
        Exit For ' found the record, no need to keep looking
    End If
Next

答案 1 :(得分:0)

我创建了以下内容:

    Sub Main()
    Dim studentID As String
    Dim studentname As String
    Dim classname As String

    Dim addScoreSelection As String

    Dim inputSID As String

    Dim Filename As String
    Filename = “Students.txt”
    FileOpen(1, Filename, OpenMode.Append)

    Console.WriteLine("Add a Score? Y/N")
    addScoreSelection = Console.ReadLine.ToLower()

    If addScoreSelection = "Y".ToLower() Then
        FileClose(1)
        Console.WriteLine("Enter a student ID: ")
        inputSID = Console.ReadLine()
        AddScore(inputSID)
    ElseIf addScoreSelection = "N".ToLower() Then
        Console.WriteLine("Student Setup")
        Console.WriteLine()
        Console.WriteLine()

        Console.Write("Student ID:  ")
        studentID = Console.ReadLine
        Console.Write("Student Name:  ")
        studentname = Console.ReadLine
        Console.WriteLine()
        Console.Write("Class  :  ")
        classname = Console.ReadLine
        WriteLine(1, studentID, studentname, classname)

        Console.WriteLine("Student successfully setup.")
    Else
        Console.WriteLine("No selection made. Exiting.")
        Process.GetCurrentProcess.Kill()
    End If


End Sub

Sub AddScore(ByVal ID As String)
    Dim Filename As String
    Filename = “Students.txt”

    Dim fileContent As String()

    Dim index As Integer = 0

    fileContent = IO.File.ReadAllLines(Filename)

    For Each line As String In fileContent
        If line.Contains(ID) Then
            fileContent(index) = fileContent(index) & ",""6""" 'Pass in a ByVal param for this
            Exit For
        End If
        index += 1
    Next

    IO.File.WriteAllLines(Filename, fileContent)
End Sub

我添加了子例程AddScore,它将学生ID作为字符串参数,然后我在.Contains()中使用此参数来确定我在文本文件中点击了我们感兴趣的行。

我通过读取文件中的所有文本来创建一个字符串数组

fileContent = IO.File.ReadAllLines(Filename) 

我遍历数组寻找有问题的行,一旦找到该行,我将得分追加到行尾(你可能想在这里实际使用一个参数)

For Each line As String In fileContent
        If line.Contains(ID) Then
            fileContent(index) = fileContent(index) & ",""6""" 'Pass in a ByVal param for this
            Exit For
        End If
        index += 1
    Next

然后我将字符串数组写入文件

IO.File.WriteAllLines(Filename, fileContent)

这解决了我相信的问题,但我个人对此并不满意。我不喜欢抓取大量文本并将其放入内存的想法(想象一下,如果我们正在阅读5GB文件......)。从长远来看,最好的解决方案是使用FileStream类来读/写它并找到该行并在所述行上附加文本。