VB - 搜索文本文件

时间:2013-06-08 14:19:41

标签: vb.net visual-studio-2010

我在学校做VB,我们正在搜索文本文件。但是,如果没有找到结果,我想输出“没有结果”ONCE。我已经尝试过,但它会为文本文件中的每行代码输出它。我该怎么做?

Module Module1
Dim townname, findtown, findname, teamname, coachname, phone As String
Dim choice As String
Sub Main()

    Do
        Console.WriteLine("Please pick an option:")
        Console.WriteLine("1. Search team by team name - Press 1")
        Console.WriteLine("2. Search team by town name - Press 2")
        Console.WriteLine("3. End Program - Press 3")

        choice = Console.ReadLine

        Select Case choice


            Case 1
                Console.Write("Enter the team name:")
                findname = Console.ReadLine()
                FileOpen(1, "TeamdataFile.txt", OpenMode.Input)

                Do
                    Input(1, teamname)
                    Input(1, townname)
                    Input(1, coachname)
                    Input(1, phone)

                    If teamname.ToLower = findname.ToLower Then

                        Console.WriteLine("Team: {0}, from {1}, coach: {2}, contact: {3}", teamname, townname, coachname, phone)
                    End If

                Loop Until EOF(1)
                FileClose(1)
                Console.ReadLine()

            Case 2
                Console.Write("Enter the town the team is from:")
                findtown = Console.ReadLine()
                FileOpen(1, "TeamdataFile.txt", OpenMode.Input)

                Do
                    Input(1, teamname)
                    Input(1, townname)
                    Input(1, coachname)
                    Input(1, phone)

                    If townname.ToLower = findtown.ToLower Then
                        Console.WriteLine("Team: {0}, from {1}, coach: {2}, contact: {3}", teamname, townname, coachname, phone)
                    End If

                Loop Until EOF(1)
                FileClose(1)
                Console.ReadLine()
            Case 3
                End
            Case Else
                Console.WriteLine("Invalid option - Choose option 1 or 2 or 3")
        End Select
    Loop Until choice = "1" Or choice = "2" Or choice = "3"
End Sub

结束模块

1 个答案:

答案 0 :(得分:1)

不要立即对找到的名称做出反应,只需将布尔变量设置为true,让检查继续直到文件结尾或立即中断循环。循环退出后,根据布尔值

打印结果
   Dim foundResult As Boolean

   .....
   Select Case choice
        Case 1
            Console.Write("Enter the team name:")
            findname = Console.ReadLine()
            FileOpen(1, "TeamdataFile.txt", OpenMode.Input)
            foundResult = False
            Do
                Input(1, teamname)
                Input(1, townname)
                Input(1, coachname)
                Input(1, phone)

                If teamname.ToLower = findname.ToLower Then
                    foundResult = True
                    Exit While
                End If

            Loop Until EOF(1)
            FileClose(1)
            if foundResult = True Then
                Console.WriteLine("Team: {0}, from {1}, coach: {2}, contact: {3}", teamname, townname, coachname, phone)
            else
                Console.WriteLine("There are no results"
            End if
      ......

作为旁注,你真的应该停止使用旧式的FileOpen,Input,FileClose。这些功能仅用于兼容VB6。 .NET Framework有更好的文件处理方法。您应该使用专为.NET设计的StreamReader及其方法。所有System.IO命名空间都要强大一些。