471,315 Members | 1,448 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

Need help with StreamReader

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
Feb 22 '06 #1
1 1079
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Terry Olsen | last post: by
reply views Thread by Flack | last post: by
5 posts views Thread by Justin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.