显示文本文件中除最后一行之外的所有行

时间:2018-02-16 11:44:45

标签: vb.net file eof

我已经创建了这个名为Go的2人游戏,我已经做到了这一点,以便游戏中的统计数据记录在文本文件中。如果相同的2名玩家不止一次互相比赛,他们的比赛将被记录在下一行的同一文本文件中。

我已经完成了所有这些,但现在我希望以前的比赛能够在屏幕上显示,以便玩家可以看到之前比赛的得分。我不希望显示最后一行,因为那是刚刚结束的游戏的得分。到目前为止,这是我的代码:

 Dim file As System.IO.StreamWriter
    'Imports the data from previous forms to form 3
    handikomi = Form1.handi_komi
    prisonerB = Form2.prisonerB
    prisonerW = Form2.prisonerW
    bpass = Form2.bpass
    wpass = Form2.wpass
    bstones = Form2.bstones
    wstones = Form2.wstones
    btotalscore = prisonerB + handikomi(0, 2)
    wtotalscore = prisonerW + handikomi(1, 2)

    If btotalscore > wtotalscore Then
        winnerlbl.Text = (handikomi(0, 0) & " IS THE WINNER!")
    ElseIf wtotalscore > btotalscore Then
        winnerlbl.Text = (handikomi(1, 0) & " IS THE WINNER!")
    End If

    file = My.Computer.FileSystem.OpenTextFileWriter("F:\My Go project flood fill2\Text files\" & handikomi(0, 0) & " VS " & handikomi(1, 0) & ".txt", True)
    'Displays statistics from current match
    file.WriteLine("BLACK" & "," & "WHITE" & "," & "Total Score" & "," & btotalscore & "," & wtotalscore & "," & "Prisoners" & "," & prisonerB & "," & prisonerW & "," & "Passes" & "," & bpass & "," & wpass & "," & "Stones" & "," & bstones & "," & wstones)
    file.Close()

    breakdwnlbl.Text = "                    BLACK   WHITE"
    breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Total Score" & "     " & btotalscore & "          " & wtotalscore)
    breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Prisoners" & "         " & prisonerB & "          " & prisonerW)
    breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Passes" & "             " & bpass & "          " & wpass)
    breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Stones" & "              " & bstones & "          " & wstones)


    Dim data As String       'to hold value of file line 
    Dim filename As String   'declare file
    filename = "F:\My Go project flood fill2\Text files\" & handikomi(0, 0) & " VS " & handikomi(1, 0) & ".txt"  'path to file on system

    FileOpen(1, filename, OpenMode.Input)  'open file for reading
    'try amend
    Do While Not EOF(1)
        data = LineInput(1)
        MsgBox(data)
    Loop

我认为我不应该在这种情况下使用EOF,但我不知道还有什么用,因为我还是初学者。我感谢任何帮助!

1 个答案:

答案 0 :(得分:4)

读取所有行并且跳过最后一行并不容易:

Dim allLines() As String = File.ReadAllLines(filename)
Dim allButLast = allLines.Take(allLines.Length - 1)

变量allButLastIEnumerable(Of String),包含文件的所有行,除了最后一行。

<强>更新

以下示例显示MessageBox中的每一行:

For Each line As String In allButLast
    MessageBox.Show(line)
Next
相关问题