Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

readline then move the pointer back?

Is there a function in the streamreader that allows for peek/read the next line to get more information without actually moving the iterator to the next position?

The action on the current line depends on the next line, but I want to keep the integrity of using this code block

while((Line = sr.ReadLine())!=null)
like image 828
Yang Avatar asked Dec 03 '13 19:12

Yang


People also ask

What does readline () method return?

The readline() method returns one line from the file. You can also specified how many bytes from the line to return, by using the size parameter.

Where is the pointer after a readline () command has executed?

readlines() come with the concept of a cursor. After either command is executed, the cursor moves to the end of the file, leaving nothing more to read in.

Does readline () return a string?

The readline method reads one line from the file and returns it as a string. The string returned by readline will contain the newline character at the end.


1 Answers

In principle, there is no need to do such a thing. If you want to relate two consecutive lines, just adapt the analysis to this fact (perform the line 1 actions while reading line 2). Sample code:

using (System.IO.StreamReader sr = new System.IO.StreamReader("path"))
{
    string line = null;
    string prevLine = null;
    while ((line = sr.ReadLine()) != null)
    {
        if (prevLine != null)
        {
            //perform all the actions you wish with the previous line now
        }

        prevLine = line;
    }
}

This might be adapted to deal with as many lines as required (a collection of previous lines instead of just prevLine).

like image 142
varocarbas Avatar answered Oct 01 '22 20:10

varocarbas