Results 1 to 8 of 8

Thread: Searching text files for a Number

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    57

    Smile 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.......

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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..

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    57

    Smile 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
    Its great getting the full code form people, but i need to be able to learn the reason the code works rather that just coping and pasting code. Im learning VB 05 on my own and I don’t have anyone to ask questions or for feed back if my code “…Looks messy…..” or it can be done a better way, But don’t get me wrong I aprecate the help I get from this forum but as I said …..”I need to learn the reason why…” so Please explane your code
    Thanks

    D

  5. #5
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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.

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    57

    Smile 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? 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
    Its great getting the full code form people, but i need to be able to learn the reason the code works rather that just coping and pasting code. Im learning VB 05 on my own and I don’t have anyone to ask questions or for feed back if my code “…Looks messy…..” or it can be done a better way, But don’t get me wrong I aprecate the help I get from this forum but as I said …..”I need to learn the reason why…” so Please explane your code
    Thanks

    D

  7. #7
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Searching text files for a Number

    VB Code:
    1. Dim strm As IO.StreamReader
    2.         Dim arr() As String
    3.         Dim strIN As String
    4.         Dim intScore As Int32 = 0
    5.  
    6.         Try
    7.             For Each fil As String In IO.Directory.GetFiles("C:\MyRaces", "*.txt")
    8.                 strm = New IO.StreamReader(fil)
    9.                 strIN = strm.ReadLine
    10.                 Do Until strIN Is Nothing
    11.                     arr = strIN.Split(","c)
    12.                     If arr(1).Trim = "Christijan Albers" Then
    13.                         intScore += Convert.ToInt32(arr(2))
    14.                     End If
    15.                     strIN = strm.ReadLine
    16.                 Loop
    17.                 strm.Close()
    18.             Next fil
    19.  
    20.             MessageBox.Show(intScore.ToString)
    21.         Catch ex As Exception
    22.             'Handle it
    23.         Finally
    24.             strm = Nothing
    25.         End Try
    Last edited by sevenhalo; Mar 22nd, 2006 at 10:17 AM. Reason: Editted to display score instead of each standing.

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    57

    Smile 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
    Its great getting the full code form people, but i need to be able to learn the reason the code works rather that just coping and pasting code. Im learning VB 05 on my own and I don’t have anyone to ask questions or for feed back if my code “…Looks messy…..” or it can be done a better way, But don’t get me wrong I aprecate the help I get from this forum but as I said …..”I need to learn the reason why…” so Please explane your code
    Thanks

    D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width