Thomas
Thomas

Reputation: 467

C# working with text files

I've been working on this for hours I'm to the last piece of what I'm working on. I need to figure out how restrict the loop to take values from a defined rang on text lines. Like lines 1-5, then 6-10, etc.

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

I'm ready to go to bed but don't want to sleep defeated. Any push in the right way would be appreciated.

Upvotes: 1

Views: 1813

Answers (2)

Stephan
Stephan

Reputation: 4247

If you want to use the StreamReader (e.g. because file size is expected to be large) you can do it like this:

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

Edit: And you should use your StreamReader inside a using block (code updated).

Upvotes: 0

Jamiec
Jamiec

Reputation: 136074

You can achieve this easily using LINQ's Skipdocs and Takedocs methods. Just read in your text using File.ReadAllLinesdocs then Skip and Take as necessary:

var lines = File.ReadAllLines("values.txt").Skip(5).Take(5);
//lines now has lines 6-10

If you're doing this multiple times, I would suggest splitting this up into the file access, and the linq:

var allLines = File.ReadAllLines("values.txt")
var lines6To10 = allLines.Skip(5).Take(5);

... // Later on 

var lines21To25 = allLines.Skip(20).Take(5)

As for the rest of your code.. looks like you're trying to find the min/max from the lines, which should only contain integers.

var min = int.MaxValue;
var max = int.MinValue;
foreach(var line in lines)
{
   var value = int.Parse(line);
   if(value > max)
      max = value;
   if(value < min)
      min = value;
}

This can be seen in the line example: http://rextester.com/rundotnet?code=SHP19181 (Note the different way of loading the numbers, as rextester cant load from a file. The principle is the same though)

Upvotes: 5

Related Questions