Programming Course in C# ¡Free!

More

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

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.

Output



Solution


using System;
using System.IO;

class More
{
public void ShowData(string name) 
{
try 
{ 
StreamReader file = new StreamReader(name);
string line;

int i = 0;

do
{
line = file.ReadLine();
if (line != null) 
{
if (i % 24 == 0)
Console.ReadLine();

if (line.Length > 79) 
line = line.Substring(0, 79);

Console.WriteLine(line);
}
i++;
}
while (line != null);

file.Close();
}
catch (Exception) 
{
Console.WriteLine("Error!!!");
}
}
}