Possible Duplicate:
How to read a text file reversely with iterator in C#
I am wondering if there is a way to read a text file from bottom to top without any performance penalty, the readLine, movenext approach, but reversed, is this sort of thing possible in .net?
To make matters more interesting the text file has around 100 000 lines in it, so I can't cheat with a readall, reverse...
Some more detail: I have a collection of incoming string values that is prefixed with an int ID that can be sorted. Unfortunately I get these IDs in the wrong order. The main issue is the sheer volume of the string values, and no RDBMS in the solution. So I really need a way to store the string values and then reverse the order of the storing during the processing. Text file came to mind because I don't have a better solution at this point in time.
Thanks in advance.
Why not use the ReadToEnd()
method of a StreamReader class, then work backwards...
Admittedly it is not pretty but it works, I used a byte array to create a MemoryStream
instance and the use that for the StreamReader
instance, using pointer hocus-pocus, the data is read in a backward fashion.
unsafe { byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello World! Foo wuz ere and so wuz bar"); using (MemoryStream mStream = new MemoryStream(b)) { string readStr; using (StreamReader sr = new StreamReader(mStream)) { readStr = sr.ReadToEnd(); } Console.WriteLine(readStr); fixed (char* beg = readStr) { char* p = beg + readStr.Length; while (p-- != beg) { Console.Write(*p); } } } }
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