How to read a text file reversely with iterator in C#

后端 未结 11 2016
甜味超标
甜味超标 2020-11-22 04:05

I need to process a large file, around 400K lines and 200 M. But sometimes I have to process from bottom up. How can I use iterator (yield return) here? Basically I don\'t l

11条回答
  •  無奈伤痛
    2020-11-22 04:29

    Attention: this approach doesn't work (explained in EDIT)

    You could use File.ReadLines to get lines iterator

    foreach (var line in File.ReadLines(@"C:\temp\ReverseRead.txt").Reverse())
    {
        if (noNeedToReadFurther)
            break;
    
        // process line here
        Console.WriteLine(line);
    }
    

    EDIT:

    After reading applejacks01's comment, I run some tests and it does look like .Reverse() actually loads whole file.

    I used File.ReadLines() to print first line of a 40MB file - memory usage of console app was 5MB. Then, used File.ReadLines().Reverse() to print last line of same file - memory usage was 95MB.

    Conclusion

    Whatever `Reverse()' is doing, it is not a good choice for reading bottom of a big file.

提交回复
热议问题