首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用文本文件的C#

使用文本文件的C#
EN

Stack Overflow用户
提问于 2011-10-10 09:23:46
回答 1查看 1.8K关注 0票数 1

我已经做了好几个小时了--我已经是我工作的最后一部分了。我需要弄清楚如何将循环限制为从文本行上定义的rang中获取值。就像第1-5行,第6-10行,等等。

代码语言:javascript
复制
TextReader tr = new StreamReader("values.txt");
        for (int i = 0; i <= 5; i++)
        {
            for (int a = 0; a <= 5; a++)
            {
                line = tr.ReadLine();
                **while ((line = tr.ReadLine()) != null)**
                    for (a = 0; a <= 5; a++){
                        ln = tr.ReadLine();
                        if (ln != null){
                            value = int.Parse(ln);
                            if (value > max)
                                max = value;
                            if (value < min)
                                min = value;}
                    }
                Console.WriteLine(tr.ReadLine());
                if (i % 5 == 0)
                    Console.WriteLine("The max is" + max);
                if (i % 5 == 0)
                    Console.WriteLine("The min is" + min);
                if (i % 5 == 0)
                    Console.WriteLine("-----First 5");
            }

我已经准备好上床睡觉了,但我不想睡觉失败了。任何以正确的方式推动将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2011-10-10 09:53:09

如果您想使用StreamReader (例如,因为预期文件大小很大),您可以这样做:

代码语言:javascript
复制
using (TextReader tr = new StreamReader("values.txt"))
{
    string currentLine = null;

    do
    {
        Console.WriteLine("processing set of 5 lines");

        for (int i = 0; i < 5; i++)
        {
            currentLine = tr.ReadLine();
            if (currentLine == null)
            {
                break;
            }

            Console.WriteLine("processing line {0} of 5", i);
            // your code goes here:
        }

    } while (currentLine != null);
}

编辑:和您应该在using block (代码更新)中使用StreamReader。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7710768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档