Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a StreamReader can read another line

I need to check if a line contains a string before I read it, I want to do this using a while loop something like this

    while(reader.ReadLine() != null)
    {
        array[i] = reader.ReadLine();
    }

This obviously doesen't work, so how can I do this selection?

like image 207
Jacco Avatar asked Dec 04 '12 14:12

Jacco


2 Answers

Try using the Peek method:

while (reader.Peek() >= 0)
{
    array[i] = reader.ReadLine();
}

Docs: http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx and http://msdn.microsoft.com/en-us/library/system.io.streamreader.peek.aspx

like image 144
Sean Airey Avatar answered Oct 12 '22 07:10

Sean Airey


String row;
while((row=reader.ReadLine())!=null){
    array[i]=row;
}

Should work.

like image 4
TinyPinkSaxophone Avatar answered Oct 12 '22 08:10

TinyPinkSaxophone