Thursday, June 22, 2017

Programming Challenge 12.4 - Tail Program

Example Files: tailLong.txt
                         tailShort.txt


/* Tail Program - This program asks the user for the name of a file.
    The program displays the last 10 lines of the file on the screen
    (the 'tail' of the file). If the file has fewer than 10 lines, the
    entire file is displayed, with a message indicating the entire file
    has been displayed. */

#include "Utility.h"

void menu();
void openFile();
bool isGood(fstream &tail, const string fileName);
void displayText(const vector<string>, const int numLines);

int main()
{
    menu();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: menu

    This function provides a basic menu. An introduction is
    displayed, and a function to open a file is called. If the
    user wishes, he or she can open another file, or exit the
    program.
   ********************************************************** */

void menu()
{
    char again = ' ';

    cout << "\n\tFILE TAIL PROGRAM\n\n"
          << "\tThis program displays the 'Tail of a file.' This means\n"
          << "\tthat, if a file contains more than 10 lines of text, only\n"
          << "\tthe last 10 lines will be displayed. If it contains fewer\n"
          << "\tthan 10 lines, the full text is displayed.\n\n";

    do
    {
        openFile();

        cout << "\n\tDo you wish to open another file [y/N]? ";
        cin >> again;

        while (toupper(again) != 'Y' && toupper(again) != 'N')
        {
            cout << "\tDo you wish to open another file [y/N]? ";
            cin >> again;
        }

        if (toupper(again) == 'N')
        {
            cout << "\n\tNow exiting the program ...";
        }
    } while (toupper(again) == 'Y');
}

/* **********************************************************
   Definition: openFile

    This function reads in and stores the contents of a text
    file. The contents of the file is stored in a vector of
    strings.
   ********************************************************** */

void openFile()
{
    int     numLines = 0;            /* To hold the number of lines of text */
    string  fileName = " ";           /* To hold the filename */
    string  tmpText = " ";            /* To hold text while it is read-in */
    fstream textFile;                    /* File stream object */

    vector<string> tail;                /* Vector of string objects to hold
                                                the file contents */

    cout << "\nEnter the name of the file you wish to open:\n";
    cin >> fileName;

    if (isGood(textFile, fileName))
    {
        while (getline(textFile, tmpText))
        {
            tail.push_back(tmpText);
            numLines++;       
        }
        textFile.close();

        displayText(tail, numLines);
    }
    else
    {
        cout << "\n\tERROR: Cannot open " << fileName << "\n";
    }
}

/* **********************************************************
   Definition: isGood

    This function accepts a reference to an fstream object as
    argument. The file is opened for input. The function
    returns true upon success, false upon failure.
   ********************************************************** */

bool isGood(fstream &textFile, const string fileName)
{
    textFile.open(fileName, ios::in);

    if (!textFile.fail())
    {
        return true;
    }
    else
    {
        return false;
    }
}

/* **********************************************************
   Definition: displayText

    This function accepts a vector of string objects as its
    argument. If the string object holds more than ten lines
    of text, only the last ten lines are displayed. Else the
    full text, and a message indicating the entire file has
    been displayed, is output to screen.
   ********************************************************** */

void displayText(const vector<string> tail, const int numLines)
{
    size_t index = 0;

    /* This ternary operator determines whether the number of lines
        is greater than 10. If it is, 'index' gets tail.size() - 10,
        or the index position of the 10th from the last line, else it
        gets 0. */
    numLines > 10 ? index = tail.size() -10 : index;

    cout << "\n\n\tFILE TAIL:\n\n";
    for (; index < tail.size(); index++)
    {
        cout << "\t" << tail[index] << "\n";
    }

    if (numLines < 10)
    {
        cout << "\n\tThe entire file has been displayed.\n";
    }
}

Example Output:




No comments:

Post a Comment