tags:

views:

167

answers:

6

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.

A: 

The ReadBlock method of the StreamReader class lets you read from a specific position. Not sure about the performance, you might want to just read it into an array, then reverse it.

Avindra Goolcharan
Would do, except I am beyond the point of loading it all into a collection due to memory constraints and high volumes, unfortunately.
JL
ReadBlock lets you read *into* a specific position in the array it's writing to. It doesn't let you specify the index to read *from* within the reader.
Jon Skeet
A: 

Well, if you're willing to dive deep you could presumably use a binary reader and do your own reading...

You'd have to figure out the line endings in reverse, and figure out how to make sure you have the right encoding.

Pretty daunting I'd say.

John Weldon
You're right, it's a pain. Quite interesting though :)
Jon Skeet
lol +1 on your original answer :)
John Weldon
A: 

Just a general answer. If there's some sort of peek() method, you could probably set your file pointer to filesize (or filesize - 1?) and then decrement the pointer until you reach 0. There is may be some class in .NET that abstracts this out. Of course, you have to define some sort of buffer size (basically define the size of the "chunks" of the file you are reading in). In that case, the filesize - 1 above might become filesize - bufferSize, and you would be incrementing your file pointer by bufferSize also.

Vivin Paliath
A: 

I think you can do this can use the PInvoke to do this via the WINAPI. You need to create a memory mapped file, but only map the end of the file and then go upwards.

Preet Sangha
+1  A: 

So you get the values in the wrong order but want to retrive them in the correct order. Maybe a

Stack<T> 

Would work for you.

http://msdn.microsoft.com/en-us/library/3278tedw.aspx

Represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.

Cory Charlton
Thanks, thats great knowledge but unfortunately I originally populate my list using a series of batch calls, otherwise that would work great.
JL
+1  A: 

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);
            }
        }
    }
}

Hope this helps, Best regards, Tom.

tommieb75
dang! just posted this before it got closed when I was testing the code... sigh
tommieb75