Read the previous line of the text file; if the current line contains "X",
At this point I have a file that is being read for a specific record "X". Once I find this entry, I can do what I need to do on the following lines, but there is some information that takes 2 lines before the "X" appears. Right now I can use var line = reader.ReadLine();
to move forward in the file and read the lines that appear after the "X". How do I go back to read this data 2 lines earlier?
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (line.Contains("X"))
{
Processing
}
}
+3
source to share
5 answers
Just keep the last 2 lines.
Fixed size queue that automatically overrides old values ββwith new parameters
0
source to share
Try to keep the previous line in memory so you can access it when you need it. Something like that:
var prev;
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (line.Contains("X")) {
// Processing both line and prev
}
prev = line; // remember previous line for future use
}
-1
source to share