I'm having trouble understanding StreamReader and ReadLine().

  • Thread starter Thread starter Mike Joseph
  • Start date Start date
M

Mike Joseph

I have a data file that was produced in MicroSoft Access.
It's a table that was exported as a fixed format,
sequential access ASCII file. It contains over 80,000
records. Basically, all the stats for every player in
Major League Baseball history in alphabetical order and
then by each year they played. I want to take this data
and display to the user the career totals for each player.

I don't seem to understand how to use StreamReader and the
Readling method. When I write out what I want to do with
the program either thru flowchart or just logical doodles
on some paper it seems pretty easy. I just can't get seem
to get it started by reading the file.

My idea was to read the data into an array. Each player
would occupy a row of the array and every time i read in a
new record for that player it would just add the new
yearly stats to the previous total to get the overall
total. So the finished array would have 15,000 or so rows
( one for each player ) and about 15 or so collums with
their career stats. Then it would be easy to display them
when the user selects a player. I just can;t figure out
how to get the data into an array in the order I'd want it
to be in :/

-Mike
 
Hi Mike,

I'd recommend you read the data not into an array, but rather into a
permanent table, either in Access or MS SQL (or any other sql database). If
you do this, you can develop indices, etc so that data is more manageable
and far more easy to access continuously. By reading it into an array, you
have to read everything every time you want to do anything. If I wants
Willie Mays only, it would be far easier to open a table where lname =
"Mays" and fname = "Willie".

Now to the real problem - if you want to use a streamreader to read the
file, that's fine - what you have to do is know the delimiters and/or column
(field) sizes; then read only the first column's data, append a row in the
new table and assign the value of that column - say, 1996 hits - to the new
table's 'hits' column. You have to do this for all columns - at bats, bases
on balls, errors, year, team with, etc - until you reach a chr(13) and
chr(10) - end of line. Then you add that row and open a new empty - and
continue until the job is done. For this you won't want to use readline, as
it will read the full line - all columns - to the end of a row. Instead,
use the read method, which will allow you to read character by character.

HTH,

Bernie Yaeger
 
Howdy Mike,

Some points need clarifying.
80000 records
15000 lines with 15 columns = 225000
??

The lines are in fixed format Ascii. What does that look like? It could be
fixed width fields or comma/tab/space delimited. Are there any quotes. Can
there be quote swithin quotes. A few example lines would be good.

Assuming that the file is comma delimited:

<code>
Public Function alReadCommaDelimitedFile _
(sFilePath As String) As ArrayList

Dim alRecords As New ArrayList
Dim strmDataFile As StreamReader

Try
strmDataFile = New StreamReader (sFilePath)
Do
Dim sLine As String = strmDataFile.ReadLine
If sLine Is Nothing Then
Exit Do
End If
alRecords.Add (sLine.Split (New Char() {","}))
Loop

Finally
strmDataFile.Close
End Try

Return alRecords
End Function
</code>

This will produce an ArrayList of string arrays. In other words each item
in alRecords will itself be an array.

You'ld use it like this:

alBaseballStats = alReadCommaDelimitedFile _
("C:\Somewhere\BaseballStats.txt")
Dim asRecord0 As String() = alBaseballStats(0)

Now asRecord0 will be an array of strings containing the 15 data items for
record 0.

The following will demonstrate. Try it on a file containing just a <few>
records.

<code>
Sub TestBaseballReading
Dim alBaseballStats As ArrayList
alBaseballStats = alReadCommaDelimitedFile _
("C:\Tmp\SHORT_BaseballStats.txt")

Dim I As Integer
Dim S As String

Dim asRecord As String()
For Each asRecord In alBaseballStats
I = I + 1
S = S & vbCrLf & I & ": "

Dim sField As String
For Each sField In asRecord
S = S & sField & ", "
Next
Next

MsgBox (S)
End Sub
</code>


Of course, if your file isn't comma-delimited, ha ha, you've at least got
a routine to put in your library.
Happy statistifying. :-)

Regards,
Fergus
 
Hi Mike,
Why d'nt you take a different approach and use the OLEDB data adapter, that
works on MS access with VB2003
With that you can get partial database information from your computer, the
array is the file.
The part of the array you want to use is the dataset and the data in the way
you want to show the array it is the dataview
When you use the IDE you will probably need more adapters, but that is I
think not important,
My advise, take a look for it.
I hopes this helps you a little bit.
Cor
 
Hello,

Mike Joseph said:
I have a data file that was produced in MicroSoft Access.
It's a table that was exported as a fixed format,
sequential access ASCII file. It contains over 80,000
records. Basically, all the stats for every player in
[...]
I just can;t figure out
how to get the data into an array in the order I'd want it
to be in :/

http://www.connectionstrings.com
-> "Text"
 
Thanks. I've been fooling around with the Read method but still not sure
how to read a specific set of characters of a string. Let's assume for a
moment I do want to put the data into an array.

Dim a as Integer
Dim b as Integer
Dim c as Integer = 500
Dim d as Integer = 33
Dim MyArray(c, d) as String
Dim datInput as New StreamReader("stats.txt")

'I would set up a loop in order to read in the data
'something like this.

MyArray(a, b) = datInput.Read()

This reads just the first character of the file, right?
Is there a way to define a specific range for the Read method? I know
that the player first name will always be characters 20 thru 39. Can I
use the read method to read just those characters? Or perhaps I could
use Readline() and then select just what I need from that line. Or even
use Read() and then concantenate then individual characters X amount of
times.

-mike
 
Mike Joseph said:
Thanks. I've been fooling around with the Read method but still not
sure how to read a specific set of characters of a string. Let's
assume for a moment I do want to put the data into an array.

Dim a as Integer
Dim b as Integer
Dim c as Integer = 500
Dim d as Integer = 33
Dim MyArray(c, d) as String
Dim datInput as New StreamReader("stats.txt")

'I would set up a loop in order to read in the data
'something like this.

MyArray(a, b) = datInput.Read()

This reads just the first character of the file, right?
Is there a way to define a specific range for the Read method? I
know that the player first name will always be characters 20 thru 39.
Can I use the read method to read just those characters? Or perhaps I
could use Readline() and then select just what I need from that line.
Or even use Read() and then concantenate then individual characters X
amount of times.

As Fergus already mentioned, could you please post some lines from the file?
What's the delimiter between records and fields?
 
Hello,

Mike Joseph said:
Thanks. I've been fooling around with the Read method but
still not sure how to read a specific set of characters of a
string. Let's assume for a moment I do want to put the data
into an array.

Dim a as Integer
Dim b as Integer
Dim c as Integer = 500
Dim d as Integer = 33
Dim MyArray(c, d) as String
Dim datInput as New StreamReader("stats.txt")

'I would set up a loop in order to read in the data
'something like this.

MyArray(a, b) = datInput.Read()

This reads just the first character of the file, right?

Basic code for reading the lines of a file:

\\\
Imports System.IO
..
..
..
Dim sr As New StreamReader("C:\WINDOWS\WIN.INI")
Dim strLine As String
strLine = sr.ReadLine()
Do Until strLine Is Nothing
MsgBox(strLine)
strLine = sr.ReadLine()
Loop
sr.Close()
///

For each line ('strLine') you can use the 'Split' method of the 'Strings'
class to split it into its components based on the delimiter. You can use
an 'ArrayList' object to store the records read from the file.
 
Back
Top