More - C# Programming Exercise

In this exercise of C#, you need to create a program that behaves like the Unix "more" command. The program should display the contents of a text file and prompt the user to press Enter each time the screen is full. As a simple approach, you can display the lines truncated to 79 characters and stop after every 24 lines. The purpose of this exercise is to learn how to handle text files and how to control the screen output interactively. Additionally, you will develop skills to create programs that simulate common command-line tools, which can be useful in applications that need to display large amounts of data in a controlled manner.

Through this exercise, programmers will learn how to handle user input and work with file reading in blocks, emulating the behavior of the Unix more command. This is a practical skill applicable in the development of console tools.

 Category

File Management

 Exercise

More

 Objective

Create a program which behaves like the Unix command "more": it must display the contents of a text file, and ask the user to press Enter each time the screen is full.

As a simple approach, you can display the lines truncated to 79 characters, and stop after each 24 lines

 Write Your C# Exercise

// Importing necessary namespaces for file handling
using System;
using System.IO;

public class More
{
    // Method to display the contents of a text file with pagination
    public static void DisplayFile(string fileName)
    {
        // Using StreamReader to read the file line by line
        using (StreamReader reader = new StreamReader(fileName))
        {
            int lineCount = 0;  // Counter to keep track of lines displayed in a "page"

            // Loop through each line in the file
            while (!reader.EndOfStream)
            {
                // Reading a line from the file and truncating it to 79 characters if needed
                string line = reader.ReadLine();
                if (line.Length > 79)
                    line = line.Substring(0, 79);

                // Displaying the line on the screen
                Console.WriteLine(line);
                lineCount++;  // Incrementing the line counter

                // Checking if 24 lines have been displayed
                if (lineCount == 24)
                {
                    // Pausing and waiting for the user to press Enter to continue
                    Console.WriteLine("Press Enter to continue...");
                    Console.ReadLine();

                    lineCount = 0;  // Resetting the line counter for the next page
                }
            }
        }
    }
}

// Auxiliary class to test the More functionality
public class Program
{
    public static void Main()
    {
        // Prompting the user to enter the name of the file to display
        Console.Write("Enter the file name to display: ");
        string fileName = Console.ReadLine();

        // Checking if the file exists before attempting to display
        if (File.Exists(fileName))
        {
            // Displaying the contents of the file with pagination
            More.DisplayFile(fileName);
        }
        else
        {
            // Displaying an error message if the file does not exist
            Console.WriteLine("The file does not exist.");
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of File Management

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Text replacer

    In this exercise of C#, you need to create a program to replace words in a text file, saving the result into a new file. The program should take as parameters the fil...

  •  Count letters in a file

    In this exercise of C#, you need to create a program to count how many times a specific character appears inside a file of any kind. The file and the letter to search...

  •  Reading a binary file (1: BMP)

    In this exercise of C#, you need to create a program that checks if a BMP image file seems to be correct. The program must verify if the first two bytes of the file ...

  •  Writing to a binary file

    In this exercise of C#, you need to create a program that asks the user for their name, age (as a byte), and the year they were born (as an int), and st...

  •  C# to Java

    In this exercise of C#, you need to create a basic C# to Java translator. The program should accept C# source files and generate an equivalent ...

  •  Invert a text file

    This C# exercise aims to teach how to manipulate text files and reverse their content using basic programming structures. In this exercise, you need to create ...