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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I test for the end of a file using a stream object? 1

Status
Not open for further replies.

skhoury

IS-IT--Management
Nov 28, 2003
386
US
Hello all,

I have a simple loop built to read lines out of a text file...I have no way of testing if I've reached the end of hte file however. Here is the code:

StreamReader stream = new StreamReader(filename, true);
currentLine = stream.ReadLine(); // Get first line (headers)

headers = currentLine.Split('\t');

while (true)
{
currentLine = stream.ReadLine();
MessageBox.Show(currentLine);
values = currentLine.Split('\t');
}

doing it this way simply crashed out at the 'values = ' statement.

I didn't see any other methods to test for the end.

Any thoughts?

Many thanks!

Sam
 
try this
Code:
StreamReader stream = new StreamReader(filename, true);
currentLine = stream.ReadLine(); // Get first line (headers)

headers = currentLine.Split('\t');

while stream.Peek() != -1
{
  currentLine = stream.ReadLine();
  MessageBox.Show(currentLine);
  values = currentLine.Split('\t');
}
 
Perfect! Works like a charm.

Thanks!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top