Question

Hello I am building a console application in VB.NET that reads a record file and outputs it to the user. I have gotten the program to output all of the records to the console but I cannot seem to get the search function working.

I want the user to input the record number and for the program to search the text file for that specific record and then output it to the console.

I will leave the read record function here for reference.

Read Records function:

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
Was it helpful?

Solution

I'm not sure exactly what you want, but here are a few examples.

To read a record number from the console and output that record, do something like this:

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

I'm not sure how else you would identify the record, but, for example, you can search the records for a value of "abc" in the first field like this:

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

Replace records(i, 1) with records(i, 0) to search for a record number.

If you want to search each field, you can add a nested loop:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top