views:

113

answers:

6

Hi,

I'm trying to read out and interpret a string line per line. I took a look at the StringReader class, but I need to find out when I'm on the last line. Here's some pseudocode of what I'm trying to accomplish:

while (!stringReaderObject.atEndOfStream()) {
    interpret(stringReaderObject.readLine());
}

Does somebody know how to do this?

Thanks!

Yvan

+3  A: 

The StreamReader class has an EndOfStream property. Have you looked into using that?

Dismissile
+3  A: 

I am not sure where you are reading from. If it is from the Console, you can check for the end of inputs string like "quit" to denote end of input

String input;
while((input = reader.readLine()) != "quit"){
   // do something
}
+3  A: 

Like this:

string line;
while (null != (line = reader.ReadLine()) {
    Process(line);
}
SLaks
No. Like this: `while(true) { string line = reader.ReadLine(); if(line == null) { break; } Process(line); }`. That single line of code is doing too much.
Jason
@Jason - not really. Also it's the canonical pattern of use in every example I've ever seen.
Kev
@Kev: It's like a bad habit passed from generation to generation.
Jason
@Jason - no it's not. It's succinct and entirely readable. You're taking single responsibility way too far.
Kev
@Kev: Since this has nothing to do with SRP, I'm not. This is mostly about readability, and to a certain extent maintainability. Succinct code is not necessarily readable code.
Jason
@Jason - there's nothing hard to read about that code. Hell, I'm an ex-VB/Clipper programmer and could understand that when I started out with .NET. That extra `if()` is just a distraction.
Kev
@Kev: I didn't say it is hard to read.
Jason
+4  A: 

Check for null when you do a readLine - see the docs.

SB
+2  A: 

If you are reading it in from a file, it is easier to do just do:

foreach(var myString in File.ReadAllLines(pathToFile))
    interpret(myString);

If you are getting the string from somewhere else (a web service class or the like) it is simpler to just split the string:

foreach(var myString in entireString.Split(Environment.NewLine))
    interpret(myString);
klausbyskov
I think this was the easiest solution to my problem. Thanks!
Yvan JANSSENS
what if his string has \r\n new lines and he's on Unix?
SB
@SB then I'm sure he would have added the "mono" and "interop" tags ;-)
klausbyskov
Worth mentioning is `File.ReadLines` (.Net 4.0) which returns a `IEnumerable<string>` so you get lines while reading the file which is better in two ways: 1. You don't have the whole line-array in memory if you don't need it. 2. `ReadAllLines` reads the whole file before you get a single line where `ReadLines` will give you the first line as soon as it is read. (more responsive). Because he is calling a method on each line which interprets it, he properly wants `ReadLines` and not `ReadAllLines`.
lasseespeholt
A: 

Dismissile is correct:

http://msdn.microsoft.com/en-us/library/system.io.streamreader.endofstream.aspx

while (!reader.EndOfStream) 
{ 
    string line = reader.ReadLine(); 
} 
Kirit Chandran