Friday, February 2, 2018

Programming Challenge 15.10 - File Double-Spacer

Example Files: FileDoubleSpacer.7z

Original Text:            WarIsKind.txt
Double-Spaced Text: doubleWar.txt 

FileFilter.h


#ifndef FILE_FILTER_H_
#define FILE_FILTER_H_

#include <fstream>
using std::ifstream;
using std::ofstream;

#include <string>
using std::string;

class FileFilter
{
    protected:
        ifstream in;                // A ifstream object
        ofstream out;                // A ofstream object

    public:
        FileFilter()
        { }

        // Constructor
        FileFilter(const string &, const string &);        // Defined in FileFilter.cpp
       
        // Destructor
        virtual ~FileFilter();                                    // Defined in FileFilter.cpp   

        // Mutator function
        void doFilter(ifstream &, ofstream &);   

        // Pure virtual function   
        virtual char transform(char) const = 0;           
};

#endif

FileFilter.cpp


#include "FileFilter.h"

#include <cstdlib>

#include <iostream>
using std::cout;

/* **********************************************************
            FileFilter::FileFilter() const string &,
                                             const string &
    The constructor accepts two string objects, holding the
    names of the input and output file to be opened. If the
    operation is successful, the files are opened. In case of
    an error, a message is output, and the program will exit.
   ********************************************************** */

FileFilter::FileFilter(const string &fNameIn, const string &fNameOut)
{
    in.open(fNameIn);

    if (!in)
    {
        cout << "\nFile read error: " << fNameIn << " could not be opened!\n\n"
              << "Make sure the filename is written correctly, that the file\n"
              << "is not damaged, and in the same folder as this program!\n";
        cout << "This program will now exit ...\n\n";
        exit(1);
    }

    out.open(fNameOut);

    if (!out)
    {
        cout << "\nFile write error: " << fNameOut << " could not be created "
              << "or opened!\n\n";
       cout << "Make sure that the file or folder is not write protected!\n";
        cout << "This program will now exit ...\n\n";
        exit(1);
    }
}

/* **********************************************************
            FileFilter::doFilter() : ifstream &, ofstream &
    This function reads in a file, transforms its contents,
    and writes the result to an output file.
   ********************************************************** */

void FileFilter::doFilter(ifstream &in, ofstream &out)
{
    char ch = ' ';                    // Holds a single character
    char transChar = ' ';        // Holds a transformed character

    in.get(ch);

    while (!in.fail())
    {
        transChar = transform(ch);

        if (transChar == '\n')
        {
            out.put(transChar);
            out.put(transChar);
        }
        else
        {
            out.put(transChar);
        }
   
        in.get(ch);       
    }
}

/* **********************************************************
            v FileFilter::~FileFilter() - Destructor
    The destructor closes the input and output files.
   ********************************************************** */

FileFilter::~FileFilter()
{
    in.close();
    out.close();
}

DoubleSpacer.h


#ifndef DOUBLE_SPACER_H_
#define DOUBLE_SPACER_H_

#include "FileFilter.h"

// DoubleSpacer class derived from the FileFilter class
class DoubleSpacer : public FileFilter
{
    public:
        DoubleSpacer() : FileFilter()
        { }

        DoubleSpacer(const string &fNameIn, const string &fNameOut) :
        FileFilter(fNameIn, fNameOut)
        {
            doFilter(in, out);
        }

        ~DoubleSpacer()
        { }

        // Overridden transform function
        // Returns a character as is
        virtual char transform(char ch) const
        {
            return ch;
        }
};

#endif

FileFilterDm.cpp


#include "DoubleSpacer.h"

#include <iostream>
using std::cin;
using std::cout;

int main()
{
    string fNameIn = "";
    string fNameOut = "";

    cout << "FILE FILTER - DOUBLE SPACER DEMO\n\n"
          << "This program allows you to:\n\n"
          << "   To add add an additional space between any two\n"
          << "   lines of text in a text file.\n\n";

    cout << "Enter the name of the file to read in:  ";
    cin >> fNameIn;

    // Demonstrates the derived Encrypt class
    cout << "\nDOUBLE SPACER\n\n";

    cout << "Enter a file name to write the double-spaced text to: ";
    cin >> fNameOut;

    // Allocate an instance of the derived class DoubleSpacer
    DoubleSpacer *spacer = new DoubleSpacer(fNameIn, fNameOut);
    cout << "Your file has been double-spaced.\n\n";
    delete spacer;

    cout << "Thank you for trying the File Filter - "
             "Double Spacer Demo. Have a nice day!";

    cin.get();
    cin.ignore();
    return 0;
}

Example Output:




No comments:

Post a Comment