Results 1 to 6 of 6

Thread: [RESOLVED] Reading and then splitting information

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Resolved [RESOLVED] Reading and then splitting information

    I have a CSV file, comma delimited, three fields and unknown rows as this constantly changes...

    I want to break apart the files, and have slightly more control over them. Basically I want to remove the first field, and then do as I please with the other two fields.

    Atm I have this code:

    VB Code:
    1. Dim reader As StreadReader = File.OpenText("file.txt")
    2. Dim line As String
    3. Dim a() As String
    4. Dim j As Integer
    5. Dim Tester As String
    6. Dim LineCount As Integer = 0
    7. While Not (reader.ReadLine() Is Nothing)
    8.   LineCount += 1
    9. End While
    10. For j = 0 To LineCount
    11.   line = reader.ReadLine()
    12.   a = line.Split(",")
    13.   Tester = Tester & a(1) & ","
    14.   Tester = Tester & a(2) & vbNewLine
    15. Next
    16. reader.Close()
    17. myfield.Text = Tester

    Now I get an error when I load this code... Before I tried to make it work with multiple lines, it worked fine. Is anyone able to help me find the problem?

    Many thanks

  2. #2
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Bath, England
    Posts
    411

    Re: Reading and then splitting information

    VB Code:
    1. While Not (reader.ReadLine() Is Nothing)
    2.   LineCount += 1
    3. End While
    4.  
    5. For j = 0 To LineCount
    6.   line = reader.ReadLine()
    You're reading the whole file and counting the lines, and then trying to read it again in the loop below it. Since you are already at the end of the file the line "line = reader.Readline()" will likely be causing the error. Look for a command to return to the start of the streamreader, or otherwise just close + reopen it before starting the loop.

  3. #3
    Lively Member matt3011's Avatar
    Join Date
    May 2002
    Location
    France
    Posts
    82

    Re: Reading and then splitting information

    You try to read When there's no more lines :

    VB Code:
    1. While Not (reader.ReadLine() Is Nothing)
    2.   LineCount += 1
    3. End While
    This piece of code read your file to the end

    So I suppose you get an error on this line :
    VB Code:
    1. Tester = Tester & a(2) & vbNewLine as a(2) is nothing

    Maybe you should do this (I don't know if it's the best way to do it, but it works:

    VB Code:
    1. Dim reader As StreamReader = File.OpenText("file.txt")
    2. Dim line As String
    3. Dim a() As String
    4. Dim Tester As String
    5. Dim LineCount As Integer = 0
    6.  
    7. Tester =""
    8. line = reader.ReadLine()
    9. While Not (line Is Nothing)
    10.   a = line.Split(",")
    11.   Tester = Tester & a(1) & ","
    12.   Tester = Tester & a(2) & vbNewLine
    13.    LineCount += 1
    14. End While
    15. reader.Close()
    16. myfield.Text = Tester

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: Reading and then splitting information

    OK I changed the code to this:

    VB Code:
    1. Dim reader As StreadReader = File.OpenText("file.txt")
    2. Dim line As String
    3. Dim a() As String
    4. Dim j As Integer
    5. Dim Tester As String
    6. Dim LineCount As Integer = 0
    7. While Not (reader.ReadLine() Is Nothing)
    8.   line = reader.ReadLine()
    9.   a = line.Split(",")
    10.   Tester = Tester & a(1) & ","
    11.   Tester = Tester & a(2) & vbNewLine
    12. End While
    13. reader.Close()
    14. myfield.Text = Tester

    And I am still getting the error, which is:

    An unhandled exception has occured in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will be shut down immediately.

    Object reference not set to an instance of an object.



    Any ideas?

    [EDIT]

    matt3011 - I just tried your code, and it makes my application crash, no errors
    Last edited by Cyber-Drugs; Jul 18th, 2005 at 05:43 AM.

  5. #5
    Lively Member matt3011's Avatar
    Join Date
    May 2002
    Location
    France
    Posts
    82

    Re: Reading and then splitting information

    Oops I forgot 1 line and it was an endless loop :
    VB Code:
    1. Dim reader As StreamReader = File.OpenText("file.txt")
    2. Dim line As String
    3. Dim a() As String
    4. Dim Tester As String
    5. Dim LineCount As Integer = 0
    6.  
    7. Tester =""
    8. line = reader.ReadLine()
    9. While Not (line Is Nothing)
    10.   a = line.Split(",")
    11.   Tester = Tester & a(1) & ","
    12.   Tester = Tester & a(2) & vbNewLine
    13.    LineCount += 1
    14.    [B]line = reader.ReadLine()[/B]
    15. End While
    16. reader.Close()
    17. myfield.Text = Tester
    This should work now

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: Reading and then splitting information

    Perfect!

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