|
-
Dec 15th, 2003, 05:04 PM
#1
Thread Starter
Lively Member
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:
If File.Exists(outputpath & dir.ToLower & ".txt") Then
'append to file
sr = File.AppendText(outputpath & dir.ToLower & ".txt")
sr.WriteLine(filename & ";" & filedate & ";" & dir & ";" & filesize)
sr.Close()
' Sort file by filename
Dim sortfile() As String
Dim inputfile As New StreamReader(outputpath & dir.ToLower & ".txt")
Dim inputstring As String
inputstring = inputfile.ReadLine()
Dim i As Integer = 0
While (inputstring <> Nothing)
Try
sortfile(i) = inputstring
Catch ex As Exception
MessageBox.Show(ex.Message, "Caught Exception")
End Try
i = i + 1
inputstring = inputfile.ReadLine()
End While
inputfile.Close()
System.Array.Sort(sortfile)
Dim outputfile As New StreamWriter(outputpath & dir.ToLower & ".txt")
Dim j As Integer = 0
While j < sortfile.Length
outputfile.WriteLine(sortfile(j))
j = j + 1
End While
outputfile.Close()
Else
'create new file
sr = File.CreateText(outputpath & dir.ToLower & ".txt")
sr.WriteLine(filename & ";" & filedate & ";" & dir & ";" & filesize)
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:
sortfile(i) = inputstring
Is this not allowed?
-
Dec 15th, 2003, 07:35 PM
#2
I wonder how many charact
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:
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.
-
Dec 16th, 2003, 08:14 AM
#3
Thread Starter
Lively Member
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.
-
Dec 16th, 2003, 09:08 AM
#4
Thread Starter
Lively Member
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:
If File.Exists(outputpath & dir.ToLower & ".txt") Then
'append to file
sr = File.AppendText(outputpath & dir.ToLower & ".txt")
sr.WriteLine(filename & ";" & filedate & ";" & dir & ";" & filesize)
sr.Close()
' Sort file by filename
'Dim sortfile() As String
Dim inputfile As New StreamReader(outputpath & dir.ToLower & ".txt")
Dim inputstring As String
Dim sortfile() As String
inputstring = inputfile.ReadLine()
Dim i As Integer = 0
While (inputstring <> Nothing)
Try
ReDim Preserve sortfile(i) 'I just added this
sortfile(i) = inputstring
Catch ex As Exception
MessageBox.Show(ex.Message, "Caught Exception")
End Try
i = i + 1
inputstring = inputfile.ReadLine()
End While
inputfile.Close()
System.Array.Sort(sortfile)
Dim outputfile As New StreamWriter(outputpath & dir.ToLower & ".txt")
Dim j As Integer = 0
While j < sortfile.Length
outputfile.WriteLine(sortfile(j))
j = j + 1
End While
outputfile.Close()
Else
'create new file
sr = File.CreateText(outputpath & dir.ToLower & ".txt")
sr.WriteLine(filename & ";" & filedate & ";" & dir & ";" & filesize)
sr.Close()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|