【问题标题】:Console VB.NET: File Processing - Search file for a specific number and output record控制台 VB.NET:文件处理 - 搜索特定编号的文件并输出记录
【发布时间】:2013-01-26 21:17:42
【问题描述】:

您好,我正在 VB.NET 中构建一个控制台应用程序,它读取记录文件并将其输出给用户。我已经让程序将所有记录输出到控制台,但我似乎无法让搜索功能正常工作。

我希望用户输入记录号,让程序在文本文件中搜索该特定记录,然后将其输出到控制台。

我把读取记录功能留在这里以供参考。

读取记录功能:

Public Function Read_Records()

    File_Name = "drecords.txt"
    File_num = FreeFile()
    Record_Counter = 0
    record_no = 999

    If File_Name <> "" Then
        Try
            FileOpen(File_num, File_Name, OpenMode.Input)
            Do Until EOF(File_num)
                Record_Counter = Record_Counter + 1
                record_no = record_no + 1
                records(Record_Counter, 0) = record_no
                records(Record_Counter, 1) = LineInput(File_num)
                records(Record_Counter, 2) = LineInput(File_num)
                records(Record_Counter, 3) = LineInput(File_num)
                records(Record_Counter, 4) = LineInput(File_num)
                records(Record_Counter, 5) = LineInput(File_num)

            Loop
            record_ID = Record_Counter

        Catch ex As Exception
            MsgBox("ERROR OPENING FILE")
        Finally
            FileClose(File_num)
        End Try
    End If
    Last_Record = Record_Counter

    Return records

End Function

【问题讨论】:

    标签: vb.net visual-studio-2010 console procedural-programming file-processing


    【解决方案1】:

    我不确定你到底想要什么,但这里有几个例子。

    要从控制台读取记录编号并输出该记录,请执行以下操作:

    dim i, k as integer
    k = val(console.readline())
    for i = 1 to 5
      console.writeline(records(k, i))
    next i
    

    我不确定您还能如何识别记录,但是,例如,您可以在记录中搜索第一个字段中的“abc”值,如下所示:

    For i = 1 to Last_Record
      if records(i, 1) = "abc" then
        ' output the record to the user
      end if  
    next i
    

    records(i, 1) 替换为records(i, 0) 以搜索记录号。

    如果要搜索每个字段,可以添加嵌套循环:

    For i = 1 to Last_Record
      for k = 1 to 5
        if records(i, k) = "abc" then
          ' output the record to the user
        end if  
      next k
    next i
    

    【讨论】:

    • 非常感谢,我改编了你的第二个样本,它成功了!
    • @RopinKain 如果 xpda 的回答回答了您的问题,您应该单击答案左侧的复选标记,这会将您的问题标记为已回答并给您和 xpda 一些代表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2015-07-26
    • 2019-04-19
    • 1970-01-01
    相关资源
    最近更新 更多