473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
Nov 20 '05 #1
7 9664
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

"Mike Joseph" <l3********@yahoo.com> wrote in message
news:58****************************@phx.gbl...
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

Nov 20 '05 #2
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
Nov 20 '05 #3
Cor
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
Nov 20 '05 #4
Hello,

"Mike Joseph" <l3********@yahoo.com> schrieb:
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"

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #5
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #6
"Mike Joseph" <l3********@yahoo.com> schrieb
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?

--
Armin

Nov 20 '05 #7
Hello,

"Mike Joseph" <l3********@yahoo.com> schrieb:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet

Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy Mee | last post by:
Hello one and all, I'm developing an Asp.NET system to take a CSV file uploaded via the web, parse it, and insert the values into an SQL database. My sticking point comes when I try to split()...
9
by: ShadowOfTheBeast | last post by:
Hi, I have got a major headache understanding streamReader and streamWriter relationship. I know how to use the streamreader and streamwriter independently. but how do you write out using the...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
7
by: Eric | last post by:
I am trying to save the "last read" position of a file using a StreamReader object. I am reading the lines of the file using StreamReader.ReadLine and then saving the current position in the...
1
by: Rob T | last post by:
Hi, I have a simple little program that I'm going to use to access our SMTP server. The below code works perfectly fine, but my question is if I were to try to execute the last line...
3
by: curt.bathras | last post by:
I am trying to open and read a file using the following: BufferedStream stream = new BufferedStream(File.OpenRead(aFilename)); If the file specified by aFilename is being used by another...
2
by: Eddy | last post by:
I have a big problem with streamreader ReadLine()! I read from a long text files about 13k lines, than I encounter a problem: ReadLine() is not anymore able to go on! I have a string whose name is...
4
by: somequestion | last post by:
Question 1. i am using StreamReader Class like this... string str = string.Empty; StreamReader sr = new StreamReader(fsIn,Encoding.Default) while ((str = sr.ReadLine()) != null) { // wanna get...
0
by: Bomac8 | last post by:
I have a text file with a four field array like this: DET-01-002737,DET-01-002737,YES,64239764b32fefc915a593f41bdb5730. My program sorts them in the order where the 3rd field says "Yes" or "NO". ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.