Searching text files for a Number
Hi people
Ok i have 17 - 19 textfiles all with racing scores on them, there are set out like this
1,Christian Klien,10,Sunny,9/03/2006
2,Christijan Albers,8,Sunny,9/03/2006
3,David Coulthard,6,Sunny,9/03/2006
4,Felipe Massa,5,Sunny,9/03/2006
5,Fernando Alonso,4,Sunny,9/03/2006
6,Giancarlo Fisichella,3,Sunny,9/03/2006
7,Jacques Villeneuve,2,Sunny,9/03/2006
8,Jarno Trulli,1,Sunny,9/03/2006
9,Jenson Button,0,Sunny,9/03/2006
11,Kimi Raikkonen,0,Sunny,9/03/2006
12,Mark Webber,0,Sunny,9/03/2006
13,Michael Schumacher,0,Sunny,9/03/2006
14,Nick Heidfeld,0,Sunny,9/03/2006
15,Nico Rosberg,0,Sunny,9/03/2006
16,Ralf Schumacher,0,Sunny,9/03/2006
17,Rubens Barrichello,0,Sunny,9/03/2006
18,Scott Speed,0,Sunny,9/03/2006
19,Takuma Sato,0,Sunny,9/03/2006
20,Tiago Monteiro,0,Sunny,9/03/2006
21,Vitantonio Liuzzi,0,Sunny,9/03/2006
22,Tiago Monteiro,0,Sunny,9/03/2006
23,Vitantonio Liuzzi,0,Sunny,9/03/2006
24,Yuji Ide,0,Sunny,9/03/2006
If i wanted to search through all 19 files to add up the score for "Christijan Albers" his score is "8" in the above text.
I would want to display the result in a textbox or lable
Thanks guys
D ;)
Please Read my Signature.......
Re: Searching text files for a Number
You could use I/O operations for this but it would be easier to use ADO.NET. You can create an OleDbCommand to retrieve just the score for the specific name you want. You can then call ExecuteScalar on that Command repreatedly using a different file as the table each time. You then just add the result of ExecuteScalar to a running total each time and once you've read each file you're left with the total. See www.connectionstrings.com for info on how to use ADO.NET with text files.
Re: Searching text files for a Number
And, since I love string manipulation, you can do it using a streamreader as well... read eac line in, splitting on the comma, and the index 1 of the resulting array is the name, see if it equals what you are searching for, if so, the index 2 value is the one you are wanting to add... then loop for the other files..
Re: Searching text files for a Number
Thanks jmcilhinney for your help but as im using streamreader already in my program i thing i will stick to it. ;)
gigemboy, about Array's do i split each textfile into an array? i ask this because im not sure about Array's yet i understand that an Array holds data but given my current problem, i'm not sure i understand how i would split 19 or so file into an array then split each line into an Array. It seem's to me that there are going to be a lot of Arrays and a hell of a lot of code or am i just looking at Array's the wrong way?
Cheers
D ;)
Please read my Signature..............Ta
Re: Searching text files for a Number
Hey D, since gig's off; I'll see if I can help you a little.
The good news is, all you'll need is 1 array and a nested loop. Everything else is splitting and string manipulation. As of right now, do you have any code assembled for this? It will be easier and more beneficial to you if we explain how to incorporate an array into your program; rather then throwing together the entire snippet.
Re: Searching text files for a Number
Hey sevenhalo
Thanks for the reply
I don’t have any code for this part of the program as such, because I’m not sure how to write it? :blush: But I will try and explain what I’m doing……..
I want to be able to enter scores from each race in the F1 season into a number of textboxs, each text box has a Combobox that I select the drivers name and Position, all this is saved in 1 textfile with the textgile’s name saved as the race circuit’s name .(this part of the program is complete)
When I click “Standings” button a groupbox is displayed with all the drivers Names & Total Scores (set out like a datagrid) form here I going to attempt to work out how many points a driver needs to win the season based on the number of races left
I have 19 – 20 textfiles (they are as above (1 file per race))
I want to be able to total all the scores for ‘Christijan Albers’ (from all the files)
Display the result in a label or textbox when the form loads
Hope this helps…
D :)
Re: Searching text files for a Number
VB Code:
Dim strm As IO.StreamReader
Dim arr() As String
Dim strIN As String
Dim intScore As Int32 = 0
Try
For Each fil As String In IO.Directory.GetFiles("C:\MyRaces", "*.txt")
strm = New IO.StreamReader(fil)
strIN = strm.ReadLine
Do Until strIN Is Nothing
arr = strIN.Split(","c)
If arr(1).Trim = "Christijan Albers" Then
intScore += Convert.ToInt32(arr(2))
End If
strIN = strm.ReadLine
Loop
strm.Close()
Next fil
MessageBox.Show(intScore.ToString)
Catch ex As Exception
'Handle it
Finally
strm = Nothing
End Try
Re: Searching text files for a Number
Ok thanks for that i will be looking into your code and see if i can understand what its doing.
Again Thanks
D ;)