0

Does anyone know how to seek to one previous line when using StreamReader, but I have lines with different length?

I have large txt file, over dozens of GBs, and I need to read it backward for one line from middle.

Thanks.

Imag Vi
  • 23
  • 6
  • *over GB* is not that large for modern computers (I mean RAM), why not `var result = File.ReadLines(@"c:\myFile.txt").TakeWhile(line => IsNotMiddle(line)).Reverse();` – Dmitry Bychenko Apr 17 '18 at 11:29
  • couldn't you first process the file with some tool before reading it ? – Pac0 Apr 17 '18 at 11:29
  • Thanks @DmitryBychenko for answering. Yes, that's true, my mistake, I meant large with over dozens of GBs, and I need to read that in chunks with acceptable speed. Thanks for your example, will try this. – Imag Vi Apr 17 '18 at 11:35
  • @Imag Vi: *dozens* of GB will ruin my example. Could you, please, provide mode details? What is the file, why you should read it in reverse rorder? What is the middle? – Dmitry Bychenko Apr 17 '18 at 11:36
  • reading backwards is not a design feature of streams/readers; to do this *efficiently*, you'd need to pre-index the line start positions - either *every* line, or every *n'th line* (and then work forwards from there) – Marc Gravell Apr 17 '18 at 11:39
  • @DmitryBychenko I'm making some kind of text editor like notepad for large txt files in WPF. And for that I use algorithm with reading small parts od file. For example, read first 100 lines and put that in my TextBox, and after I scroll down ten I need to read next 10 lines and that is easy. Also, when I scroll up, I need to read previous 10 lines, and there is a problem. I mentioned middle of file just for example. – Imag Vi Apr 17 '18 at 11:41
  • Thanks @MarcGravell for answering, I added one comment about my task and why need this. – Imag Vi Apr 17 '18 at 11:47
  • @ImagVi then again, I'd suggest: index the stream position, say, every 100 lines; if you need to jump to line 12315, then: lookup the position of line 12300 from your index, seek the stream to there, and read 15 lines forward – Marc Gravell Apr 17 '18 at 11:49
  • There's code in this answer that could be useful to you: https://stackoverflow.com/questions/452902 – Jon Skeet Apr 17 '18 at 18:27
  • Thanks @JonSkeet I took a look on this part of code, and it is useful – Imag Vi Apr 18 '18 at 07:07

1 Answers1

-1

You can use "File.ReadAllLines" function, this is the fastest way I know to read text file. For example, I have a text file like below.

aaa123
aaa456
aaa789
bbb123
bbb456
bbb789

When I search in each lines in text file if I found "a789". So I expect to get the data of previous line. In this case is "aaa456".

   string[] lines = File.ReadAllLines(@"C:\Users\Binh\Desktop\C#\test\test.txt");           
   string data = "";

   for (int i = 0; i < lines.Length; i++)           
   {
      if(lines[i].Contains("a789"))
      {
          data = lines[i - 1];//Read backward for one line
      }
   }        
   Console.WriteLine(data);//This will return "aaa456"