473,685 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9684
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********@yah oo.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 alReadCommaDeli mitedFile _
(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.Re adLine
If sLine Is Nothing Then
Exit Do
End If
alRecords.Add (sLine.Split (New Char() {","}))
Loop

Finally
strmDataFile.Cl ose
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 = alReadCommaDeli mitedFile _
("C:\Somewhere\ BaseballStats.t xt")
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 TestBaseballRea ding
Dim alBaseballStats As ArrayList
alBaseballStats = alReadCommaDeli mitedFile _
("C:\Tmp\SHORT_ BaseballStats.t xt")

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********@yah oo.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("s tats.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********@yah oo.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("s tats.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********@yah oo.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("s tats.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.I NI")
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
12231
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() the string returned by readline() on the file. The following code snippet works for me: tokens = "one,two,three,four".Split(",") for each token in tokens response.write("<td>"+token+"</td>")
9
4586
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 streamwriter, what you have read into a streamReader? and also can someone explain how they work in simple terms -- The Matrix Insurrection
21
13081
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 server is on one line (\r\n at end) and is formed as - each of which is seperated by a space. Arguments with spaces in them are enclosed in quotations. So, I'm able to open a connection to the server. When I send a message to
7
22403
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 file. My problem is that on subsequent reads, the starting position is not correct. I have tried many different ways to increment the file position value, but I cannot get it to work so that on the next read, the read starts at the correct...
1
4361
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 (info=streamReader.ReadLine) another time, the program hangs. Most likely since the SMTP server has no other responses to post. Is there a way to make the streamreader time out if there is no other info? BTW, the only code not posted is ConnectSocket...it...
3
4542
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 application, an IOException is thrown with the message: "The process cannot access the file because it is being used by another process." I figured since I opened the file for read only using File.OpenRead, I would be fine... but no such luck.
2
8491
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 riga, where I put the return value of Readline(), and this is what happens, when I read these three lines: 106933;;rosso;ivan mauricio;3347614603;trak15@yahoo.com;1984-06-04;M;;;milano;CO;politecnico # ingegneria;;;...
4
17918
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 line Count of Sr } i just want to get a count of sr.
0
1082
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". "YES" means it isa parent, "NO" means it is the child of that parent as long as field 2 of the parent is equal to field 2 of the child. Forn some reason my code is printing some of the children multiple times. example of some of the output: ...
0
8577
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8926
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7589
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6431
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2938
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.