Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read text file into array

Status
Not open for further replies.

Corinne

Programmer
May 15, 2001
146
US
Could someone please explain to me how to accomplish the following? I need to read in a file containing the following data structure:

BEGIN REQUEST DATA
FName: First
MNname: Middle
LName: Last
Address: 101 Street Address
City: MYCity
State/Province: New York
END REQUEST DATA

BEGIN REQUEST DATA
FName: First
MNname: Middle
LName: Last
Address: 101 Street Address
City: MYCity
State/Province: New York
END REQUEST DATA

What my question is is how do I move onto the next block of data as I move through the file? So far I have a routine that reads through the file and places all of the items into an array. After that it parses the data into an object but I'm not sure how to write the code to move to the next block of information.

Thanks,
Corinne
 
Code:
Public Structure Person
  Dim FName As String
  Dim MNname As String
  Dim LName As String
  Dim Address As String
  Dim City As String
  Dim State_Province As String
End Structure

Function Whatever() As Integer

  Dim myReader as New StreamReader("Your File Name")

  Dim tmpStr as String

  ' If you don't know how many records you could do the following to get it

  Dim lineCount As Integer = 1

  tmpStr = myReader.ReadLine

  Do While Not tmpStr Is Nothing
    tmpStr = myReader.ReadLine
    lineCount  += 1
  Loop

  myReader.Close

  ' Then to get record count, you have 9 lines per record

  Dim noRecs as Integer = lineCount / 9
  
  Dim Data(noRecs + 1) as Person ' Add 1 just to make sure you have a big enough array

  'Then read it in

 myReader = New StreamReader("Your File Name")

  tmpStr = myReader.ReadLine
  Dim i, j as Integer

  For j = 0 to lineCount 

   If tmpStr = "END REQUEST DATA" Then
      i += 1   '  Means you found the end of record set and add 1 for your array

    ElseIf tmpStr.StartsWith("FName:") Then
      Data(i).Fname = tmpStr.Substring(7)

    ' Then add in your other elseif's to get the other details.
    End If

   tmpStr = myReader.ReadLine

  Next

  myReader.Close

  End Function
This is just a quick procedure that could be used, it's not checked to see if it works, but it's the basic idea.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top