Results 1 to 4 of 4

Thread: reading a text file to an array - problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92

    reading a text file to an array - problem

    This seems so easy, but I'm pulling my hair out because I can't figure it out. Maybe some fresh eyes will be able to spot my glaringly simple mistake that I just can't find.

    I want to read a text file in, line by line, sort the lines, and write back to the same text file. But I'm having problems assigning the line that is read from the file to an element of the array. Here's my code:

    VB Code:
    1. If File.Exists(outputpath & dir.ToLower & ".txt") Then
    2.                 'append to file
    3.                 sr = File.AppendText(outputpath & dir.ToLower & ".txt")
    4.                 sr.WriteLine(filename & ";" & filedate & ";" & dir & ";" & filesize)
    5.                 sr.Close()
    6.                 ' Sort file by filename
    7.                 Dim sortfile() As String
    8.                 Dim inputfile As New StreamReader(outputpath & dir.ToLower & ".txt")
    9.                 Dim inputstring As String
    10.                 inputstring = inputfile.ReadLine()
    11.                 Dim i As Integer = 0
    12.                 While (inputstring <> Nothing)
    13.                     Try
    14.                         sortfile(i) = inputstring
    15.                     Catch ex As Exception
    16.                         MessageBox.Show(ex.Message, "Caught Exception")
    17.                     End Try
    18.  
    19.                     i = i + 1
    20.                     inputstring = inputfile.ReadLine()
    21.                 End While
    22.                 inputfile.Close()
    23.                 System.Array.Sort(sortfile)
    24.                 Dim outputfile As New StreamWriter(outputpath & dir.ToLower & ".txt")
    25.                 Dim j As Integer = 0
    26.                 While j < sortfile.Length
    27.                     outputfile.WriteLine(sortfile(j))
    28.                     j = j + 1
    29.                 End While
    30.                 outputfile.Close()
    31.             Else
    32.                 'create new file
    33.                 sr = File.CreateText(outputpath & dir.ToLower & ".txt")
    34.                 sr.WriteLine(filename & ";" & filedate & ";" & dir & ";" & filesize)
    35.                 sr.Close()

    I keep getting the exception
    Object reference not set to an instance of an object
    when reading the file in, right at the line:
    VB Code:
    1. sortfile(i) = inputstring

    Is this not allowed?

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You haven't set the size of the array SortFile, only its reference.

    Therefore, you have only declared to the compiler to set aside 2 bytes to point to a spot in memory where to begin the array.

    But the array size is never declared, therefore, it fails.

    You have to declare the size of the array when you declare the reference.
    VB Code:
    1. Dim SortFile(myinputfile.Lines) As String

    So you will need a way to count the number of lines in your file to continue on the approach you are using.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    Ok, I see.

    Is there a better way to go about this? I'm going to have to do something similar, yet slightly different (so I can't reuse this exact code) later in the program.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    I seem to have found a way around this.

    Just before the assignment statement, sortfile(i) = inputstring, I put in Redim Preserve sortfile(i), so that it looks like this (Note the Try/Catch statement):

    VB Code:
    1. If File.Exists(outputpath & dir.ToLower & ".txt") Then
    2.                 'append to file
    3.                 sr = File.AppendText(outputpath & dir.ToLower & ".txt")
    4.                 sr.WriteLine(filename & ";" & filedate & ";" & dir & ";" & filesize)
    5.                 sr.Close()
    6.                 ' Sort file by filename
    7.                 'Dim sortfile() As String
    8.                 Dim inputfile As New StreamReader(outputpath & dir.ToLower & ".txt")
    9.                 Dim inputstring As String
    10.                 Dim sortfile() As String
    11.                 inputstring = inputfile.ReadLine()
    12.                 Dim i As Integer = 0
    13.                 While (inputstring <> Nothing)
    14.                     Try
    15.                         ReDim Preserve sortfile(i)      'I just added this
    16.                         sortfile(i) = inputstring
    17.                     Catch ex As Exception
    18.                         MessageBox.Show(ex.Message, "Caught Exception")
    19.                     End Try
    20.  
    21.                     i = i + 1
    22.                     inputstring = inputfile.ReadLine()
    23.                 End While
    24.                 inputfile.Close()
    25.                 System.Array.Sort(sortfile)
    26.                 Dim outputfile As New StreamWriter(outputpath & dir.ToLower & ".txt")
    27.                 Dim j As Integer = 0
    28.                 While j < sortfile.Length
    29.                     outputfile.WriteLine(sortfile(j))
    30.                     j = j + 1
    31.                 End While
    32.                 outputfile.Close()
    33.             Else
    34.                 'create new file
    35.                 sr = File.CreateText(outputpath & dir.ToLower & ".txt")
    36.                 sr.WriteLine(filename & ";" & filedate & ";" & dir & ";" & filesize)
    37.                 sr.Close()
    38.             End If

    This works. I get a sorted text file, but is this the best way to do it, or is there a more elegant way?

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