Friday, October 23, 2015

C# Reading and Storing File Information in an Array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Ranking
{
    class Program
    {
        static void Main(string[] args)
        {
            //keep a count of the number of lines
            string line;

            //Array to store each line infomation
            string[] nameArray = File.ReadAllLines("c:\\output.txt");

            //read the file information in and store it in file
            StreamReader file = new StreamReader("c:\\output.txt");

            //loop no more than the number of lines in the document
            for (int i = 0; i  < nameArray.Length; i++)
            {
                //if the file is not read the information and print it
                if ((line = file.ReadLine()) != null)
                {
                    Console.WriteLine(i + " " + line);
                }
            }
            Console.ReadLine();

            file.Close();
        }
    }
}

No comments:

Post a Comment