Need help with StreamReader

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there!

I need to read the contents of a text file into a string array. I must be
doing something wrong because my try...catch goes straight to the catch. Here
is major part of my code:

try
{
using (StreamReader sr = new StreamReader(@"C:\Test.dat"))
{
string line;
String[] temp = null;
int index = 0;
while ((line = sr.ReadLine()) != null)
{
temp[index] = line;
index += index;
}
MessageBox.Show(temp[0]);
}
}
catch
{
// Let the user know what went wrong.
MessageBox.Show("OOPS!");
}

I am pretty sure that this would work in Fortran but not here in C# 2005.
The length of the DAT file is unknown so I declared my array without bounds.
I think I am reading each line correctly into the array and then want to test
if it worked with the MessageBox.

Any help or comments would be appreciated. Thanks.

Sean
 
Sean,
String[] temp = null;
int index = 0;
while ((line = sr.ReadLine()) != null)
{
temp[index] = line;

The problem is that you're trying to use the temp array variable which
is null. Since you don't know the number of lines you probably wand to
use an ArrayList (or List<string>) instead.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top