C++: Please write code for following problem: A file filter reads an input file,
ID: 3574484 • Letter: C
Question
C++: Please write code for following problem:
A file filter reads an input file, transforms it in some way, and writes the results to an output file. For this assignment, you will write multiple classes to create a file filter.
First of all create an abstract file filter class that defines a pure virtual function for transforming a character. The class should have the following member function that should be a pure virtual function and should be called to perform the actual filtering.:
void doFilter(ifstream &in, ofstream &out)
From that class, create three classes. All three will be derived from the abstract class. The three classes hould do the following:
Create one derived class of your file filter class that performs encryption
The encryption class should have a constructor that takes an integer as an argument and uses it as the encryption key (modify the ASCII value by that amount).
Create another derived class that transforms a file to all uppercase
The member function for transforming a single character should have the prototype:
char transform(char ch)
Create a third derived class that creates an unchanged copy of the original file.
Other than the encryption class there should be no attributes, constructors or destructors. The main thing here is to redefine the pure virtual function.
Explanation / Answer
//main.cpp
#include <fstream>
#include <iostream>
#include "FileFilter.h"
#include "CopyFileFilter.h"
#include "EncryptFileFilter.h"
#include "UpcaseFileFilter.h"
#include "GroupEncryptFileFilter.h"
int main()
{
std::ifstream copyInStream;
std::ofstream copyOutStream;
//test that we can open the file
copyInStream.open("inFile.txt");
if(!copyInStream.good())
{
std::cout << "File 'inFile.txt' cannot be found!" << std::endl;
}
else
{
copyInStream.close();
}
//Copy the file
copyInStream.open("inFile.txt");
copyOutStream.open("copyFileOut.txt");
CopyFileFilter *copyFilter = new CopyFileFilter();
copyFilter->doFilter(copyInStream, copyOutStream);
copyInStream.close();
copyOutStream.close();
delete copyFilter;
//encrypt the file
copyInStream.open("inFile.txt");
copyOutStream.open("encryptFileOut.txt");
EncryptFileFilter *encryptFilter = new EncryptFileFilter(5);
encryptFilter->doFilter(copyInStream, copyOutStream);
copyInStream.close();
copyOutStream.close();
delete encryptFilter;
//upcase the file
copyInStream.open("inFile.txt");
copyOutStream.open("upcaseFileOut.txt");
UpcaseFileFilter *upcaseFilter = new UpcaseFileFilter();
upcaseFilter->doFilter(copyInStream, copyOutStream);
copyInStream.close();
copyOutStream.close();
delete upcaseFilter;
//group encrypt
copyInStream.open("inFile.txt");
copyOutStream.open("groupEncryptFileOut.txt");
GroupEncryptFileFilter *groupEncryptFilter = new GroupEncryptFileFilter(5);
groupEncryptFilter->doFilter(copyInStream, copyOutStream);
delete groupEncryptFilter;
return 0;
}
=====================================================================
//UpcaseFileFilter.cpp
#include "UpcaseFileFilter.h"
const int LOWER_TO_UPPER = -32;
char UpcaseFileFilter::transform(char ch)
{
if((int)ch >= 97 && (int)ch <= 122)
{
return (char)((int)ch + LOWER_TO_UPPER);
}
else
{
return ch;
}
}
========================================================================
//UpcaseFileFilter.h
#pragma once
#include "FileFilter.h"
class UpcaseFileFilter : public FileFilter
{
private:
virtual char transform(char ch);
};
===================================================================
//GroupEncryptFileFilter.cpp
#include "GroupEncryptFileFilter.h"
#include <fstream>
GroupEncryptFileFilter::GroupEncryptFileFilter(int encryptKey)
{
this->encryptKey = encryptKey;
}
//we need to do more than the base class does, so we have to define a new implementation
void GroupEncryptFileFilter::doFilter(std::ifstream &in, std::ofstream &out)
{
this->charCounter = 0;
while((in.peek() != EOF))
{
char inChar;
char outChar;
if(this->charCounter >= 5)
{
out.put(' ');
this->charCounter = 0;
continue;
}
else if(in.peek() == ' ' || in.peek() == ' ')
{
//grab the input and throw it away
in.get(inChar);
continue;
}
else
{
in.get(inChar);
outChar = transform(inChar);
out.put(outChar);
this->charCounter++;
}
}
}
char GroupEncryptFileFilter::transform(char ch)
{
if(((int)ch >= 65 && (int)ch <= 90) || ((int)ch >= 97 && (int)ch <= 122))
{
return (((((int) ch) + this->encryptKey) % 26) + 65);
}
else
{
return ch;
}
}
===============================================================================
//GroupEncryptFileFilter.h
#pragma once
#include "EncryptFileFilter.h"
class GroupEncryptFileFilter : public FileFilter
{
public:
virtual void doFilter(std::ifstream &in, std::ofstream &out);
GroupEncryptFileFilter(int encryptKey);
private:
int encryptKey;
int charCounter;
virtual char transform(char ch);
};
==================================================================
//FileFilter.cpp
#include "FileFilter.h"
//assumes that the stream has been opened
void FileFilter::doFilter(std::ifstream &in, std::ofstream &out)
{
while((in.peek() != EOF))
{
char inChar;
char outChar;
in.get(inChar);
outChar = transform(inChar);
out.put(outChar);
}
}
=======================================================================
//FileFilter.h
#pragma once
#include <fstream>
class FileFilter
{
public:
virtual void doFilter(std::ifstream &in, std::ofstream &out);
private:
virtual char transform(char ch) = 0;
};
================================================================
//EncryptFileFilter.cpp
#include <iostream>
#include "EncryptFileFilter.h"
EncryptFileFilter::EncryptFileFilter(int encryptKey)
{
this->encryptKey = encryptKey;
}
char EncryptFileFilter::transform(char ch)
{
if((int)ch >= 65 && (int)ch <= 90)
{
return (((((int)ch) + this->encryptKey) % 26) + 65);
}
else if((int)ch >= 97 && (int)ch <= 122)
{
return (((((int)ch) + this->encryptKey) % 26) + 97);
}
else
{
return ch;
}
}
======================================================================
//EncryptFileFilter.h
#pragma once
#include "FileFilter.h"
class EncryptFileFilter : public FileFilter
{
public:
EncryptFileFilter(int encryptKey);
private:
int encryptKey;
virtual char transform(char ch);
};
===================================================================
//CopyFileFilter.cpp
#include "CopyFileFilter.h"
char CopyFileFilter::transform(char ch)
{
return ch;
}
=================================================================
//CopyFileFilter.h
#pragma once
#include "FileFilter.h"
class CopyFileFilter : public FileFilter
{
private:
virtual char transform(char ch);
};
=========================================================================
inFile.txt
testing 123
this is a test
==================================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.