none
URGENT Reading from a .txt file RRS feed

  • Question

  • How would I read from line 4 and onwards?
    Is there a way to specify from which line you want to read from?
    Sunday, March 4, 2007 3:41 PM

Answers

  • Here i a snippet from a www.codeproject.com article regarding reading lines. Below is the link. I think this can be modified to do what you are asking.

     

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;

    namespace ReadLine
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Load our text file
                TextReader tr = new StreamReader("\\test.txt");

                //How many lines should be loaded?
                int NumberOfLines = 15;

                //Make our array for each line
                string[] ListLines = new string[NumberOfLines];

                //Read the number of lines and put them in the array
                for (int i = 1; i < NumberOfLines; i++)
                {
                    ListLinesIdea = tr.ReadLine();
                }

                //This will write the 5th line into the console
                Console.WriteLine(ListLines[5]);
                //This will write the 1st line into the console
                Console.WriteLine(ListLines[1]);

                Console.ReadLine();

                // close the stream
                tr.Close();
            }
        }
    }

    http://www.codeproject.com/useritems/Read_a_certain_line.asp

     

    Best of luck,

     

    Martin

    Sunday, March 4, 2007 4:03 PM