Wednesday, June 21, 2017

Programming Challenge 12.3 - Punch Line

Example Files: joke.txt
                         punchline.txt


/* Punch Line - This program reads and prints a joke and its punch line
    from two different files. The first file contains a joke, but not its
    punch line. The second file has the punch line as its last line,
    preceded by 'garbage.' The main function opens the two files and then
    calls two functions, passing each one the file it needs. The first
    function reads and displays each line in the file it is passed (the
    joke file). The second function should display only the last line of
    the file it is passed (the punch line file). It finds this line by
    seeking to the end of the file and then backing up to the beginning
    of the last line. Data to test the program is found in the joke.txt
    and punchline.txt files. */

#include "Utility.h"

void displayJoke(fstream &);
void displayPunchline(fstream &);

int main()
{
    cout << "\n\tJOKE OF THE DAY\n\n";

    fstream joke("joke.txt", ios::in);

    if (joke.fail())
    {
        cout << "\tFile open error. Program aborting now.";
        cin.get();
        return 0;
    }
    else
    {
        displayJoke(joke);
    }
    joke.close();

    fstream pLine("punchline.txt", ios::in);

    if (pLine.fail())
    {
        cout << "\tFile open error. Program aborting now.";
        cin.get();
        return 0;
    }
    else
    {
        displayPunchline(pLine);
    }
    pLine.close();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: displayJoke

    This function accepts a reference to an fstream object as
    its argument. It reads in text from the 'joke.txt' file,
    and displays its contents.
   ********************************************************** */

void displayJoke(fstream &joke)
{
    string theJoke = " ";

    while (getline(joke, theJoke) && !joke.eof())
    {
        cout << "\t" << theJoke << "\n";
    }
}

/* **********************************************************
    Definition: displayPunchline

    This function accepts a reference to an fstream object as
    its argument. It finds the beginning of the last line in
    the file and displays it.
   ********************************************************** */

void displayPunchline(fstream &pLine)
{
    string punchLine = " ";
    char     tmpChar = ' ';
    long     position = 0;

    pLine.clear();                        /* Rewind the file                 */
    pLine.seekg(0L, ios::end);        /* Move to the end of the file */
   
    /* Starting at -1 bytes relative to the end of the file, this
        loop reads in and processes each character until it finds a
        newline character. If found, the last line in the file is
        read-in and displayed. */
    while (pLine.seekg(position-1, ios::cur))
    {   
        position--;
        pLine.get(tmpChar);

        if (tmpChar == '\n')
        {
            getline(pLine, punchLine);
            cout << "\n\t" << punchLine;
        }
    }       
}

Example Output:





No comments:

Post a Comment