.NET:有没有办法读取下一个txt文件顶部? [英] .net: Is there a way to read a txt file from bottom to top?

查看:139
本文介绍了.NET:有没有办法读取下一个txt文件顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/452902/how-to-read-a-text-file-reversely-with-iterator-in-c">How以反向读取文本文件与迭代器在C#

我想知道是否有一种方法可以从下读取文本文件顶部没有任何性能损失,readline的,MoveNext的方法,但逆转,被这样的事情有可能在.NET?

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?

为使事情更有趣的文本文件中有大约100万行,所以我不能用ReadAll方法作弊,反...

To make matters more interesting the text file has around 100 000 lines in it, so I can't cheat with a readall, reverse...

一些细节:我进来的字符串值的集合,是pfixed与可排序的int ID $ P $。不幸的是,我得到这些ID以错误的顺序。主要的问题是字符串值的绝对数量,并在解决方案中没有RDBMS。所以,我真的需要一种方法来存储字符串值,然后在加工过程中扭转存储的顺序。文本文件浮现在脑海,因为我没有这个时间点更好的解决方案。

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.

在此先感谢。

推荐答案

为什么不使用StreamReader类的 ReadToEnd()的方法,然后倒推... 诚然,它不是pretty的,但它的作品,我用一个字节数组来创建一个的MemoryStream 实例和使用,对于的StreamReader 例如,使用指针骗人的,数据读入一个落后的方式。

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.

这篇关于.NET:有没有办法读取下一个txt文件顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆