w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Read last n number of lines from a file

Secondary Nav

C# Sharp File Handling : Exercise-14 with Solution

Write a program in C# Sharp to create and read last n number of lines of a file.
Test Data and Expected Output :

 Input number of lines to write in the file  :5                                                               
 Input 5 strings below :                                                                                      
 Input line 1 : line1                                                                                         
 Input line 2 : line2                                                                                         
 Input line 3 : line3                                                                                         
 Input line 4 : line4                                                                                         
 Input line 5 : line5                                                                                         
                                                                                                              
 Input last how many numbers of lines you want to display  :3                                                  
                                                                                                              
 The content of the last 3 lines of the file mytest.txt is :                                                  
 The last no 3 line is : line3                                                                                
 The last no 2 line is : line4                                                                                
 The last no 1 line is : line5          

C# Sharp Code:

using System;
using System.IO;


class filexercise14
{
    static void Main()
    {
		string fileName = @"mytest.txt"; 
		string[] ArrLines ;
		int n,i,l,m=1;

	    Console.Write("\n\n Read last n number of lines from a file  :\n");
		Console.Write("-----------------------------------------------\n"); 

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
       Console.Write(" Input number of lines to write in the file  :");
       n= Convert.ToInt32(Console.ReadLine()); 
       ArrLines=new string[n];      
       Console.Write(" Input {0} strings below :\n",n);
		for(i=0;i<n;i++)
		{
		Console.Write(" Input line {0} : ",i+1);
		ArrLines[i] = Console.ReadLine();	
		}	
        System.IO.File.WriteAllLines(fileName, ArrLines);
		
            Console.Write("\n Input last how many numbers of lines you want to display  :");
            l = Convert.ToInt32(Console.ReadLine()); 
            m=l;
            if(l>=1 && l<=n)
            {
             Console.Write("\n The content of the last {0} lines of the file {1} is : \n",l,fileName);
			if (File.Exists(fileName))
				{
				for(i=n-l;i<n;i++)
				{
				string[] lines = File.ReadAllLines(fileName);
				Console.Write(" The last no {0} line is : {1} \n",m,lines[i]);
				m--;
				}
				}
				}
				else
				{
				  Console.WriteLine(" Please input the correct line no.");
				}

            Console.WriteLine();       
    }
}

C# Sharp Practice online:


using System;
using System.IO;


class filexercise14
{
    static void Main()
    {
		string fileName = @"mytest.txt"; 
		string[] ArrLines ;
		int n,i,l,m=1;

	    Console.Write("\n\n Read last n number of lines from a file  :\n");
		Console.Write("-----------------------------------------------\n"); 

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
       Console.Write(" Input number of lines to write in the file  :");
       n= Convert.ToInt32(Console.ReadLine()); 
       ArrLines=new string[n];      
       Console.Write(" Input {0} strings below :\n",n);
		for(i=0;i<n;i++)
		{
		Console.Write(" Input line {0} : ",i+1);
		ArrLines[i] = Console.ReadLine();	
		}	
        System.IO.File.WriteAllLines(fileName, ArrLines);
		
            Console.Write("\n Input last how many numbers of lines you want to display  :");
            l = Convert.ToInt32(Console.ReadLine()); 
            m=l;
            if(l>=1 && l<=n)
            {
             Console.Write("\n The content of the last {0} lines of the file {1} is : \n",l,fileName);
			if (File.Exists(fileName))
				{
				for(i=n-l;i<n;i++)
				{
				string[] lines = File.ReadAllLines(fileName);
				Console.Write(" The last no {0} line is : {1} \n",m,lines[i]);
				m--;
				}
				}
				}
				else
				{
				  Console.WriteLine(" Please input the correct line no.");
				}

            Console.WriteLine();       
    }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program