4 Replies - 10718 Views - Last Post: 05 February 2012 - 07:44 AM Rate Topic: -----

#1 Coffeybeanz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-December 08

Reading a text file into an array of structs.

Posted 22 March 2009 - 11:34 AM

I have an assignment where I must read the information from a text file into an array of structs, so that when a part number is input, the correct price will be displayed.

the file is a text file named shoeinfo.txt

it looks like this: BZB34,40.99
BZJ25,54.50
JMM97,14.99
JMM98,14.99
KNN12,35.99
LYR05,25.99
MMR56,7.99
PMN23,11.99
RTN21,7.99
RTZ11,8.99

my code so far is

 Public Class Form1
	Structure Product

		Public partnum As String
		Public price As Decimal

	End Structure

	Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
		Me.Close()
	End Sub

	Private Sub pricedispLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pricedispLabel.Click
		Dim search As String
		Dim subscript As Integer

		search = partnumBox.Text


	End Sub

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Dim item(9) As Product
		Dim path As String = "C:\Documents and Settings\CoffeyA\My Documents\Visual Studio 2005\Projects\Chap09\shoeinfo.txt"
		Dim text As String

		text = My.Computer.FileSystem.ReadAllText(path)



unfortunately I am unable to figure out how the code is to be read into the array.
i.e. item(0).partnum = BZB34
item(0).price = 4099D

and so on. Please help.

Is This A Good Question/Topic? 0
  • +

#5 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Reading a text file into an array of structs.

Posted 22 March 2009 - 03:17 PM

First, instead of using the ReadAllText method to import the entire file into your program all at once, i would go through it line-by-line using a IO.StreamReader, that way you can read one line at a time. To make a streamreader, use
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim item(9) As Product
    Dim path As String = "C:\Documents and Settings\CoffeyA\My Documents\Visual Studio 2005\Projects\Chap09\shoeinfo.txt"

    'Make a stream reader
    Dim reader As New System.IO.StreamReader(path)
End Sub


Then, using that stream reader, you can read the file line by line. To do that, you would need to make a while loop
While (Not reader.EndOfStream)

End While


Basically, that will loop until you have reached the end of the file. But, you are not reading the file in that loop, so it would go on forever as it is now. So, you can use the reader.ReadLine() function to import the next line from the text file until reader.EndOfStream.
While (Not reader.EndOfStream)
    Dim Current As String = reader.ReadLine()
    'current now holds a line from the text file.
End While



Now you can go through the file line by line and fill the struct array. However, you have to be able to know what line your on in the file to be able to reference the correct index in the struct array, so i would create an int variable before the while loop, and increment it in the loop.
Dim Index As Integer = -1 'start off as -1 because array indexing starts at 0
While (Not reader.EndOfStream)
    Index += 1 'add 1 to index
    Dim Current As String = reader.ReadLine()
    'current now holds a line from the text file.
End While


Now you know what index your at when your looping. Now, as this is an assignment, i don't want to do all the work for you, i'll leave you for now to see if you can figure out on your own how to parse the partnum and price from the line your on in the file. If you have any questions, fell free to ask, but try to see if you can do it yourself now that i've pointed you in the right direction.
Was This Post Helpful? 1

#6 Coffeybeanz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-December 08

Re: Reading a text file into an array of structs.

Posted 22 March 2009 - 10:15 PM

Thank you so much. That was a wonderful shove in the right direction. We had not covered the reader command as of yet, so I wasnt sure what to do there. Im going to give it a try here and Ill let you know how it works out.
Was This Post Helpful? 0
  • +
  • -

#7 Elviii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-February 12

Re: Reading a text file into an array of structs.

Posted 05 February 2012 - 07:12 AM

I also need to use this but I'm am struggling to seperate the different varibles from the streadreader and then save them into different arrays.
For example:
I would like to save all the lines in the text file which follow this pattern: John, 13, C
into 3 difference arrays.
I have attempted to use the split function and then try to save the split strings individually but so far I have been unsuccessful. Any help would be appreciated, thanks :)
Was This Post Helpful? 0
  • +
  • -

#8 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Reading a text file into an array of structs.

Posted 05 February 2012 - 07:44 AM

check out the a split method


Dim line as string = ""
Dim str() as string = nothing
for each line In product
str = line.split(","c)


Sorry didn't read you post fully i am still a bit hung over

This post has been edited by m_wylie85: 05 February 2012 - 07:48 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1