Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

C++ abstract file filter reads an input file transforms it in some way and writes the results to an output file

File Filter
A file filter reads an input file, transforms it in some way, and writes the results to an output
file. Write an abstract file filter class that defines a pure virtual function for transforming
a character. Create one derived class of your file filter class that performs encryption,
another that transforms a file to all uppercase, and another that creates an unchanged
copy of the original file. The class should have the following member function:
void doFilter(ifstream &in, of stream &out)
This function should be called to perform the actual filtering. The member funct ion
for transforming a single character should have the prototype:
char transform(char ch)
The encryption class should have a constructor that takes an integer as an argument
and uses it as the encryption key.

Answer:


// File Filter
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;

//***********************************************
// Filter class declaration                     *
//***********************************************
class Filter
{
public:
    void doFilter(fstream &in, fstream &out);
protected:
    virtual char transform(char ch) = 0;
};

//**********************************************
// The doFilter method performs the filtering. *
// *********************************************

void Filter::doFilter(fstream &in, fstream &out)
{
    char ch = in.get();
    while (ch != EOF)
    {
        out.put(transform(ch));
        ch = in.get();
    }
}

//*********************************************
// Uppercase Class Declaration. This class is *
// derived from the Filter class.             *
//*********************************************

class UpperCaseFilter: public Filter
{
protected:
    char transform(char ch)
    {
        return toupper(ch);
    }    
};


void printFile(fstream &); // Prototype

const int SIZE = 81;  // Array size for file name

//***********************************************
// The main function                            *
//***********************************************

int main( )
{
   char inFileName[SIZE];  // To hold the input file name
   char outFileName[SIZE]; // To hold the output file name
      
    // Explain the program
    cout << "This program creates an uppercase "
         << "version of a test file.\n";
    cout << "Enter name of input file (input.txt): "; cin >> inFileName;
    fstream inputFile(inFileName, ios::in);
    if (!inputFile)
    {
        cout << "The file " << inFileName
             << " cannot be opened." << endl;
        exit(EXIT_FAILURE);
    }

    // Echo the input file
    cout << "\nOriginal file is: " << endl;

    printFile(inputFile);
    cout << endl;

    cout << "Enter name of output file: "; cin >> outFileName;
    fstream outputFile(outFileName, ios::out);
    if (!outputFile)
    {
        cout << "The file " << outFileName 
             << " cannot be opened." << endl;
        exit(EXIT_FAILURE);
    }
    
    // Process 
    UpperCaseFilter upperFilter;
    upperFilter.doFilter(inputFile, outputFile);

    // Close output file and reopen for reading so we can print
    // its contents.
    outputFile.close();
    outputFile.open(outFileName, ios::in);
    cout << "\nFiltered file is: " << endl;
    printFile(outputFile);

    // Close all files
    inputFile.close();
    outputFile.close();
    return 0;    
}
    

// **************************************
// Print file function                  *
// **************************************

void printFile(fstream &file)
{
   // Start at beginning of file
   file.clear(); 
   file.seekg(0, ios::beg);

   // Print the file contents
   char ch = file.get();
   while (ch != EOF)
   {
      cout << ch;
      ch = file.get();
   }
   
   // Go back to beginning of file for further processing.
   file.clear();
   file.seekg(0, ios::beg);
}



    
    



About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!