Reading a text file thats delimited

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 3788 Views - Last Post: 16 November 2012 - 03:12 PM Rate Topic: -----

#1 summey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 19-January 09

Reading a text file thats delimited

Posted 13 November 2012 - 08:44 AM

I am trying to figure out how to read a text file that looks like this

1962@Dr. No@Sean Connery@$425,488,741
1963@From Russia with Love@Sean Connery@$555,909,803
1964@Goldfinger@Sean Connery@$868,659,354
1965@Thunderball@Sean Connery@$966,435,555
1967@You Only Live Twice@Sean Connery@$720,388,023
1969@On Her Majesty's Secret Service@George Lazenby@$513,445,231
1971@Diamonds Are Forever@Sean Connery@$617,520,987
1973@Live and Let Die@Roger Moore@$785,677,477
1974@The Man with the Golden Gun@Roger Moore@$426,826,774
1977@The Spy Who Loved Me@Roger Moore@$666,367,656
1979@Moonraker@Roger Moore@$624,527,272
1981@For Your Eyes Only@Roger Moore@$481,005,579
1983@Octopussy@Roger Moore@$405,873,493
1985@A View to a Kill@Roger Moore@$316,186,616
1987@The Living Daylights@Timothy Dalton@$362,876,056
1989@Licence to Kill@Timothy Dalton@$271,586,451
1995@GoldenEye@Pierce Brosnan@$499,954,330
1997@Tomorrow Never Dies@Pierce Brosnan@$465,588,535
1999@The World Is Not Enough@Pierce Brosnan@$504,705,882
2002@Die Another Day@Pierce Brosnan@$546,490,272
2006@Casino Royale@Daniel Craig@$640,803,677
2008@Quantum of Solace@Daniel Craig@$586,090,727

The program looks like this:

http://imgur.com/Fzsoj

Posted Image


As you can see from the image above when you select an agent from the combo box and the agents data is shown in the text box.


my code so far...

   
Dim parser As New FileIO.TextFieldParser("Program 3 Data File\Program_3_Data.txt")
        Dim rows As String()

        parser.SetDelimiters("Year@Title@Actor@Inflation")

        parser.TextFieldType = FileIO.FieldType.Delimited

        Try
            rows = parser.ReadFields

            For Each Str As String In rows
                EmployeeRecordsRichTextBox.Text = (Str)
            Next

            Do While Not parser.EndOfData
                rows = parser.ReadFields

                'Print the Results
                For Each Str As String In rows
                    Console.WriteLine(Str)
                Next
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try



Any help on how to do this would be great thank you for your time.

This post has been edited by modi123_1: 13 November 2012 - 08:51 AM
Reason for edit:: fixed media tags


Is This A Good Question/Topic? 0
  • +

Replies To: Reading a text file thats delimited

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5677
  • View blog
  • Posts: 12,211
  • Joined: 02-June 10

Re: Reading a text file thats delimited

Posted 13 November 2012 - 10:01 AM

So your question is what? Exactly. How to read a text file? How to split the lines on the "@" symbol? How to run a function when one of the checkboxes is ticked?
Was This Post Helpful? 0
  • +
  • -

#3 summey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 19-January 09

Re: Reading a text file thats delimited

Posted 13 November 2012 - 10:57 AM

View PosttlhIn`toq, on 13 November 2012 - 10:01 AM, said:

So your question is what? Exactly. How to read a text file? How to split the lines on the "@" symbol? How to run a function when one of the checkboxes is ticked?




All of the above I know they all play off each other. I am really unsure how to do all of the parsing and getting the format correct for the output like in the image.
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Reading a text file thats delimited

Posted 13 November 2012 - 11:02 AM

You know what would save you a lot of time, is create a string array and split each line.

So stream read the file, for each line of the file or during a while loop of some sort *hint hint*, declare a string array:

Dim Split() as String = Streamreader1.Readline.Split("@")



Now what do you want to do with the information? That's your bidding. Use some kind of loop to go through the string array.
Was This Post Helpful? 0
  • +
  • -

#5 summey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 19-January 09

Re: Reading a text file thats delimited

Posted 13 November 2012 - 12:38 PM

View Posttrevster344, on 13 November 2012 - 11:02 AM, said:

You know what would save you a lot of time, is create a string array and split each line.

So stream read the file, for each line of the file or during a while loop of some sort *hint hint*, declare a string array:

Dim Split() as String = Streamreader1.Readline.Split("@")



Now what do you want to do with the information? That's your bidding. Use some kind of loop to go through the string array.



Yeah that isnt working so i was able to read the text file in with this code but how do i format it?
Try
            Using sr As New StreamReader("Program 3 Data File\Program_3_Data.txt")
                Dim line As String
                line = sr.ReadToEnd()
                EmployeeRecordsRichTextBox.Text = (line)
            End Using
        Catch ex As Exception
            MessageBox.Show("The file could not be read: ", ex.Message)

        End Try


Really confused thanks for the help
Was This Post Helpful? 0
  • +
  • -

#6 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Reading a text file thats delimited

Posted 13 November 2012 - 12:50 PM

line = sr.ReadToEnd reads whole file. Use ReadLine, then store this line somewhere for later use, or split it right away with String.Split method.

Edit:
Also put your ReadLine command (and other relevant lines) to Do While sr.Peek() >= 0 .... Loop block.

This post has been edited by lucky3: 13 November 2012 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 summey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 19-January 09

Re: Reading a text file thats delimited

Posted 13 November 2012 - 04:20 PM

View Postlucky3, on 13 November 2012 - 12:50 PM, said:

line = sr.ReadToEnd reads whole file. Use ReadLine, then store this line somewhere for later use, or split it right away with String.Split method.

Edit:
Also put your ReadLine command (and other relevant lines) to Do While sr.Peek() >= 0 .... Loop block.




Try
            Using sr As New StreamReader("Program 3 Data File\Program_3_Data.txt")
                Dim line As String
                Do While sr.Peek() >= 0
                    line = sr.ReadLine()
                    EmployeeRecordsRichTextBox.Text = (line)
                Loop
            End Using
        Catch ex As Exception
            MessageBox.Show("The file could not be read: ", ex.Message)

        End Try




This is what i have been messing with but the output in the text box is

2008@Quantum of Solace@Daniel Craig@$586,090,727

thats it...
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5677
  • View blog
  • Posts: 12,211
  • Joined: 02-June 10

Re: Reading a text file thats delimited

Posted 13 November 2012 - 05:15 PM

Trevstar344 has shown you the .Split("@") but you haven't tried to use it.
If you're not going to even try the advice given - then why ask?
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Reading a text file thats delimited

Posted 13 November 2012 - 05:19 PM

Set the delimiters to @ not Year@Title@Actor@Inflation


Then add the information into a custom class

This post has been edited by AdamSpeight2008: 13 November 2012 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#10 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Reading a text file thats delimited

Posted 13 November 2012 - 06:07 PM

Split command is the best bet here, has yet to fail me. Adam's suggestion is also something I would recommend as it will also allow you to expand on possible data modification without doing a lot of work.

Dim BlahString as String = "BLAH@BLAH@BLAH@BLAH"

Dim Split() as String = BlahString.Split("@") 'Starting from 0 each element is updated with the value of each occurrence of the data following or preceding the delimiter in this string.

For Each Item as String in Split 'Go through each element..
MyRichTextBox1.text &= Item & Environment.Newline 'Adds the item and a newline to the richtextbox
Next

'Output'
'BLAH'
'BLAH'
'BLAH'
'BLAH'



Hopefully you'll try a little harder here to give it a shot.
Was This Post Helpful? 0
  • +
  • -

#11 summey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 19-January 09

Re: Reading a text file thats delimited

Posted 13 November 2012 - 07:06 PM

View Posttrevster344, on 13 November 2012 - 06:07 PM, said:

Split command is the best bet here, has yet to fail me. Adam's suggestion is also something I would recommend as it will also allow you to expand on possible data modification without doing a lot of work.

Dim BlahString as String = "BLAH@BLAH@BLAH@BLAH"

Dim Split() as String = BlahString.Split("@") 'Starting from 0 each element is updated with the value of each occurrence of the data following or preceding the delimiter in this string.

For Each Item as String in Split 'Go through each element..
MyRichTextBox1.text &= Item & Environment.Newline 'Adds the item and a newline to the richtextbox
Next

'Output'
'BLAH'
'BLAH'
'BLAH'
'BLAH'



Hopefully you'll try a little harder here to give it a shot.


I understand why you guys think i am just some lazy kid not putting in the work. I did try all of your examples and i did fail to mention that we have to use Option Strict On :\ so this is making this much more difficult and is why your examples are failing for me. Thank you for the help i do appreciate it but don't judge me... I am trying.
Was This Post Helpful? 0
  • +
  • -

#12 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Reading a text file thats delimited

Posted 13 November 2012 - 08:52 PM

That's a simple conversion. Instead of Split("@") you have to convert the string to character. Split(CChar("@")). Works fine in my compiler with Option Strict on. The compiler will give you plenty of ways to do this without having to break your back about it.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Reading a text file thats delimited

Posted 13 November 2012 - 08:58 PM

Or just use "@"c.
Was This Post Helpful? 2
  • +
  • -

#14 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Reading a text file thats delimited

Posted 13 November 2012 - 09:35 PM

I didn't even know that Adam, thanks man.
Was This Post Helpful? 0
  • +
  • -

#15 summey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 19-January 09

Re: Reading a text file thats delimited

Posted 14 November 2012 - 08:20 AM

Not sure why its not working... its like the loop is messed up or something.

I have worked with the examples you guys have given me and it only displays

2008@Quantum of Solace@Daniel Craig@$586,090,727

thats it just that one line in the richtextbox....

here is the code i came up with.


   Dim appPath As String = "Program 3 Data File\Program_3_Data.txt"
        Dim fileEntries As New List(Of String)

        If Not File.Exists(appPath) Then
            MessageBox.Show("File not found")
            Exit Sub
        End If

        Try

            Dim reader As StreamReader = New StreamReader(appPath)
            fileEntries.Clear()

            Do Until reader.Peek = -1
                fileEntries.Add(reader.ReadLine)
            Loop

            reader.Close()

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try


        For Each line As String In fileEntries


            Dim parts() As String = Split(line, "@")

            Try
                EmployeeRecordsRichTextBox.Text = line

            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try
        Next
    End Sub





Thank you all for the guidance
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3