Click here to Skip to main content
Click here to Skip to main content

Read a certain line in a text file

, 3 Nov 2005
Rate this:
Please Sign up or sign in to vote.
Read a certain line in your text files by making an array
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Introduction

This Article will show you how to read a line in a text file. We will write a small class that will take the file that you have loaded and number of lines you want to load. I tired to make this simple as possible.

The Code

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++)
            {
                ListLines[i] = 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();
        }
    }
}

The Code - Explained

I'll now take the time to let you know what is going on in the code.


            TextReader tr = new StreamReader("\\test.txt");

This will just load the text file into the program, make sure that you rename it to whatever your file is.


            int NumberOfLines = 15;
            string[] ListLines = new string[NumberOfLines];

The NumberOfLines will hold the number of lines you want to load into the array. The ListLines makes our array that will hold each line from our text file.
Note: Make sure that you add one to NumberOfLines, if you have a total of 5 lines make NumberOfLines equal 6.


            for (int i = 1; i < NumberOfLines; i++)
            {
                ListLines[i] = tr.ReadLine();
            }

This is the main part of the program that will put each line of code into the ListLines array. This will allow us to call each line whenever we want to.


            Console.WriteLine(ListLines[5]);
            Console.WriteLine(ListLines[1]);

All you have to do now is use ListLines[NumberLineYouWant] and you can do whatever you want with it.

My Small Background

This is my fist article I've made. I started C# about 2 weeks ago. This might not be the best way (For example you need to get to the 50,000th line in a text file, you'll have to make an array that will hold 50,000 lines of text). I started programming in PHP at the age of 12 (I'm 14 now). Ever since that I've always wanted to learn a new language. I've played with C++ then I started coding in C. To me C was much easier that C++. I have also played with ASP and ASP.NET, but since I was good at PHP, I couldn't find a reason for those languages. Now, here I am with C#, which to me is far easier then C or C++.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jisu74

United States United States
No Biography provided

Comments and Discussions

 
GeneralTHis code looks promising Pinmemberoffroaderdan15-Oct-09 7:47 
GeneralCould always use an Arraylist PinmemberXybex4-Nov-05 7:32 
GeneralRe: Could always use an Arraylist Pinmemberdue85014-Jan-09 17:26 
GeneralProblem with your code Pinmemberleppie3-Nov-05 22:26 
GeneralMake a class out of it PinmemberRobert Rohde3-Nov-05 19:43 
GeneralRe: Make a class out of it PinmemberJisu744-Nov-05 11:37 
GeneralRe: Make a class out of it Pinmembervangogh.vincent1-Oct-07 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.1 | Last Updated 4 Nov 2005
Article Copyright 2005 by Jisu74
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid