Monday, June 19, 2017

Programming Challenge 12.1 - File Head Program

Example files: games.txt
                         excerpt.txt


/* File Head Program - This program asks the user for the name of a
    file. The program displays the first 10 lines of the file on the
    screen (the "head" 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 &, const string);
int  lineCnt(const vector<string>);
void displayText(const vector<string>, const int);

int main()
{
    menu();

   pauseSystem();
   return 0;
}

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

    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 HEAD PROGRAM\n\n"
            << "\tThis program displays the 'Head of a file.' This means\n"
            << "\tthat, if a file contains more than 10 lines of text, only\n"
            << "\t10 lines will be displayed. If it contains fewer than 10\n"
            << "\tlines, the full text is displayed.\n\n";

    do
    {
        openFile();

        cout << "\n\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> gamesText;        /* To hold the file contents */

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

    /* Upon success, the files contents is read in and stored in the vector,
       the number of lines is counted, the file closed, and the displayText
        function is called. In case of an error, a message is displayed, and
        the program will exit. */
    if (isGood(textFile, fileName))
    {
        while (getline(textFile, tmpText) && !textFile.eof())
        {
            gamesText.push_back(tmpText);
            numLines++;
        }
        textFile.close();

        displayText(gamesText, numLines);
    }
    else
    {
        cout << "\tERROR: Cannot open the file.\n\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 first 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> gamesText, const int numLines)
{
    int output = 0;    /* To hold the number of lines to be output */

    /* This ternary operator determines whether the number of
        lines is greater than 10. If it is, 'output' gets 10,
        else it gets numLines. The purpose is to limit the output
        to screen to a maximum of 10 lines of text. */
    numLines > 10 ? output = 10 : output = numLines;

    cout << "\n\n\tFILE HEAD:\n\n";
    for (int index = 0; index < output; index++)
    {
        cout << "\t" << gamesText[index] << "\n";
    }   

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

Example Output:





No comments:

Post a Comment