Reading text file into array

DavidIQ
01-07-2005, 02:56 PM
I'm about to give up on this thing now. I've been working on it for hours and I keep getting the same error. The way I have it written I'm thinking it should work but it's not. :confused:

Here's what I'm trying to do:
There is a text file that is written by the same program that I've made. Each line of the text file has a name on it. I'd like to assign the names of this text file to an array so that I can generate a random list from the names on that text file (or from the array). I haven't gotten as far as debugging the randomizing part of the code because for some reason the array doesn't seem to want to take in the values from the sreStreamReader.ReadLine() code.

Since I have the exception catcher I'm getting an error message: "Object reference not set to an instance of an object." There is another part of the code that has a similar look and I'm getting the same error but since it's not doing a catch I'm getting an error from the IDE which causes the program to freeze. The first error is referencing what's in bold here as being the problem:

'Reads in sequential access file
Try
sreStreamReader = IO.File.OpenText("list.txt")

'Assigns each value in the file to each position in the array
Do Until sreStreamReader.Peek() = -1
List(intCount) = sreStreamReader.ReadLine()
intCount = intCount + 1
Loop

Catch ex As Exception
MessageBox.Show(ex.Message, "List", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
End Try

Here are my variables declared for this segment:

Dim intCount As Integer
Dim strAssign As String
'Array for the list
Dim List() As String

The second error is coming from more or less the same code elsewhere. Am I missing something here? I'll appreciate it if someone can decipher this beast :huh: . Thanks. :-\

MKoslof
01-07-2005, 04:03 PM
If I understand you correctly, you have a text file with data, you want to read in each line. If so, do something like:

1) Make sure you create a new instance of a streamreader object
2) Create a new ArrayList
3) Dim a string variable
4) Assign the string variable equal the .ReadToEnd method of the streamReader or read it in all at once
5) Use the .AddRange method of the arraylist to add the string, using .Split() to split the string at each new line.

Now you have an arraylist with each value.

DavidIQ
01-07-2005, 10:47 PM
I've never used an ArrayList. Is that part of System.IO? I'm not exactly understanding how the Array is getting filled. In other words I got lost at 4 and 5. Does .ReadToEnd assign the entire text file to the string variable? And does .AddRange just add each line from the file?

...And I thought I knew coding...sheesh

MKoslof
01-08-2005, 09:35 AM
Something like this:



Dim sw As New StreamReader("C:\testing.txt")
Dim s As String
Dim eachline As New ArrayList
Dim i As Long

s = sw.ReadToEnd

'now split each each new line
eachline.AddRange(s.Split(Environment.NewLine))

'loop array list, see each line value
For i = 0 To eachline.Count - 1
MessageBox.Show(eachline(i).ToString.Trim)
Next

DavidIQ
01-10-2005, 12:38 PM
Something like this:



Dim sw As New StreamReader("C:\testing.txt")
Dim s As String
Dim eachline As New ArrayList
Dim i As Long

s = sw.ReadToEnd

'now split each each new line
eachline.AddRange(s.Split(Environment.NewLine))

'loop array list, see each line value
For i = 0 To eachline.Count - 1
MessageBox.Show(eachline(i).ToString.Trim)
Next


Just got to trying this out and when I insert the code the line that says eachline.AddRange(s.Split(Environment.NewLine)) is giving me this error:

Option Strict On disallows implicit conversions from 'String' to 'Char'.

If I go ahead and compile the program anyways I get the following error for that same line of code:

String must be only one character long.

I'm playing around with it right now so I'll keep you posted if I can get it to work or if you have an idea of what's wrong I'd appreciate it if you could let me know. Thanks.

MKoslof
01-14-2005, 04:10 PM
OK, well then The Environ() call is not going to work :)

You could use the old legacy VB vbcrlf to signify a line feed/carriage return, but you should avoid old legacy calls in .Net.

While thinking about it, you should use the power of the regular expression in .Net. Basically you can read in the entire file and then use a regular expression to split the string at each line and loop the contents

There is a great regular expressions tutorial at MSDN :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum