Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a text file into an array

Status
Not open for further replies.

mohebk

MIS
Aug 23, 2005
139
US
Hi,
I am trying to read text file data into an array. I expect to get each line from the file in a record in the array. I came up with the code below but I am getting an error on the read line about "System.NullReferenceException"

Can you please help?

ReadFile = IO.File.OpenText(FileName)
For i = 1 To RecCount
dArr(i) = ReadFile.ReadLine()
Next
ReadFile.Close()



Mo
 
It will no doubt be the fact that you are using a counter to read each line (RecCount) and a certain line doesn't exist. Instead of doing that, use a While loop to read the file.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi,
I changed it to read like below and still get the same error.

ReadFile = IO.File.OpenText(FileName)
RecCount = 0
Do While (ReadFile.Peek <> -1)
ReadFile.ReadLine()
dArr(RecCount) = ReadFile.ReadLine
RecCount += 1
Loop
ReadFile.Close()

Mo
 
Now you are reading the line twice from within your While Loop...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I commented the first read but still get the same error message. This is the dim line in the form load. Do you see any thing wrong with it?

Private dArr() As String
Private RecCount As Integer

This is the read after the comment line;

ReadFile = IO.File.OpenText(FileName)
RecCount = 0
Do While (ReadFile.Peek <> -1)
'ReadFile.ReadLine()
dArr(RecCount) = ReadFile.ReadLine()
RecCount += 1
Loop
ReadFile.Close()

I really appreciate your help. As you can tell, I am a .Net begginer.

Mo
 
Try using an ArrayList instead. Taking your example, something like the followng should work:
Code:
        Dim ReadFile As System.IO.StreamReader
        Dim dArr As New ArrayList

        ReadFile = IO.File.OpenText("C:\Inetpub\[URL unfurl="true"]wwwroot\WebApplication1\Test.txt")[/URL]
        Dim RecCount As Integer = 0
        Do While (ReadFile.Peek <> -1)
            dArr.Insert(RecCount, ReadFile.ReadLine)
            RecCount += 1
        Loop
        ReadFile.Close()


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
how do you instantiate dArr?

Better use an arraylist.

Code:
[Blue]Dim[/Blue] dArr [Blue]As[/Blue] [Blue]New[/Blue] arraylist
ReadFile = IO.File.OpenText(FileName)
[Blue]Do[/Blue] [Blue]While[/Blue] (ReadFile.Peek <> -1)
    dArr.add(ReadFile.ReadLine())
[Blue]Loop[/Blue]
ReadFile.Close()

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
The error you are getting is because you are attempting to access elements of the array which don't yet exist.

Try the following:

Code:
  [Blue]Private[/Blue] dArr() [Blue]As[/Blue] [Blue]String[/Blue]
  [Blue]Private[/Blue] RecCount [Blue]As[/Blue] [Blue]Integer[/Blue]
  [Blue]Private[/Blue] [Blue]Sub[/Blue] Button7_Click([Blue]ByVal[/Blue] sender [Blue]As[/Blue] System.Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.EventArgs) [Blue]Handles[/Blue] Button7.Click

    [Blue]Dim[/Blue] FileName [Blue]As[/Blue] [Blue]String[/Blue] = [Red]"NameOfFileToRead"[/Red]
    [Blue]Dim[/Blue] readfile [Blue]As[/Blue] IO.StreamReader
    ReadFile = IO.File.OpenText(FileName)
    [b]RecCount = -1[/b]
    Do [Blue]While[/Blue] (ReadFile.Peek <> -1)
      RecCount += 1
      [b]ReDim Preserve dArr(RecCount)[/b]
      dArr(RecCount) = readfile.ReadLine()
    Loop
    readfile.Close()
    TextBox1.Text = [Red]"Total of: "[/Red] + (RecCount + 1).ToString + [Red]" lines"[/Red] + Environment.NewLine
    [Blue]For[/Blue] a [Blue]As[/Blue] [Blue]Integer[/Blue] = 0 [Blue]To[/Blue] RecCount
      TextBox1.Text += (a + 1).ToString([Red]"000"[/Red]) + [Red]": "[/Red] + dArr(a) + Environment.NewLine
    [Blue]Next[/Blue]

  [Blue]End[/Blue] [Blue]Sub[/Blue]

Hope this helps.

[vampire][bat]
 
Thanks a lot guys. chrissie1's idea worked great. I used it because it looks much more simple. How can I copy this dynamic array list to an array that will have the exact size of the array list (RecCount)?

Thanks

Mo
 
If you really do need to use an array, I think my solution is easier. To block copy the ArrayList to an Array you would need to:

Dim AnArray() As Object
AnArray = dArr.ToArray()

which leaves you with an Array of Object, not String.

Since you are using chrissie's ArrayList solution, why change it to an Array?

To access element of an ArrayList you have Count to determine its length and Item to extract data at a particular index (0 based). You also have ToString to return each Item as a String.


Hope this helps.

[vampire][bat]
 
Thanks a lot earthandfire. This is what is called a sufficient answer. It works great now.

Mo
 
Wow, that's alot of code just to read a text file. Why not just use the dynamic array to read/split at once.
Code:
Dim saArr() as string
ReadFile = IO.File.OpenText(FileName)
saArr = Split(ReadFile.ReadToEnd, vbCrLf)
ReadFile.Close
Now if you need an account of the number of lines just use the.
Code:
saArr.GetUpperBound(0)

Maybe this world is another planet’s Hell.
Aldous Huxley

eyes_015.gif___1129027102664
eyes_015.gif___1129027102664
 
Hi bigtimein,
I think the reason I am redefining the array is to resize it so to fit the required number of rows only as opposed to have one dynamic array that can allocate a lot of space in the memory.

Mo
 
bigtimmin, you are making the assumption that the textfile is produced by a Windows/DOS program. Textfiles are not all the same. Some use vbCr as the line terminator, some use vbLf, although most created by Windows applications would use vbCrLf.

ReadLine works with any of the terminators whereas using the Split fuction only works if you (the programmer) know the terminator in advance (i.e. when the program is written).

Therefore, whilst your code maybe a few lines shorter, it is not guaranteed to work. The solutions posted by both chrissie and myself are not dependent on knowing the line terminator and will therefore work with any textfile.

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top