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)
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.
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.
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.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With