Question #272759

11. File components-strings. Determine the number of characters in each line.

Expert's answer

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


namespace App
{
    class Program
    {




        public static void Main()
        {




            Console.Write("Enter the file name: ");
            string fileName = Console.ReadLine();
            //string path = @"c:\temp\MyTest.txt";
            if (File.Exists(fileName))
            {
                // Open the file to read from.
                string[] readText = File.ReadAllLines(fileName);
                int counterLine = 1;
                foreach (string s in readText)
                {
                    Console.WriteLine("The number of characters in line {0} is {1}", counterLine, s.Length);
                    counterLine++;
                }
            }
            Console.ReadLine();
        }


    }
}

LATEST TUTORIALS
APPROVED BY CLIENTS