Question

In: Computer Science

A file filter reads an input file, transforms it in some way, and writes the results...

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 subclass 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 a member function

void doFilter(ifstream &in, ofstream &out)

that is called to perform the actual filtering. The member function 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.

Be sure to include comments throughout your code where appropriate.

Complete the C++ code using Visual Studio or Xcode, compress (zip) and upload the entire project folder to the Blackboard assignment area by clicking on the Browse My Computer button or by dragging the file inside the Attach Files box.

This assignment is worth 20 points and will be evaluated using the CIS 221 Assignment Rubric, which is available from the assignment submission area, as well as in the course Grade Center.

Solutions

Expert Solution

 //contents of FileFilter.h  #ifndef FILEFILTER_H #define FILEFILTER_H #include <iostream> #include <fstream> using namespace std; class FileFilter{ public: //function doFilter  //this is a function that all the derived  //classes will call, and we can define it  //directly  void doFilter(ifstream &in, ofstream &out){ //char variable to store characters read  //and the updated one  char ch, up; //read characters from file one by one  //until no more characters to read,  //using a while loop  while(in.get(ch)){ //get the transformed character  up = transform(ch); out.put(up); } } //the pure virtual function, which will be  //overridden in the derived classes, is the  //transform function  virtual char transform(char ch) = 0; }; #endif
 //contents of Upp.h  #ifndef UPP_H #define UPP_H #include <iostream> using namespace std; class Upp : public FileFilter{ public: virtual char transform(char ch){ return toupper(ch); } }; #endif
 //contents of Encrypt.h  #ifndef ENCRYPT_H #define ENCRYPT_H #include <iostream> using namespace std; class Encrypt : public FileFilter{ private: int key; public: //default constructor  Encrypt(){ key = 10; } //overloaded constructor  Encrypt(int k){ key = k; } //remember that chars are represented  //as integers, of their ASCII values  virtual char transform(char ch){ return ch + key; } }; #endif
 //contents of CopyFile.h  #ifndef COPYFILE_H #define COPYFILE_H #include <iostream> using namespace std; class CopyFile : public FileFilter{ public: virtual char transform(char ch){ return ch; } }; #endif
 //contents of main.cpp  #include <iostream> #include <fstream> #include "FileFilter.h" #include "Upp.h" #include "Encrypt.h" #include "CopyFile.h" using namespace std; int main() { //define an ifstream object to read contents  //of input file, and open it directly  cout << "Now opening input file for reading...\n"; ifstream inputFile("inputFile.txt"); cout << "Done! \"inputFile.txt\" open for reading!\n"; //define an ofstream object to open  //output file, first for the uppercase  //file filter  cout << "\nOpening output file for uppercase filter...\n"; ofstream outputFile("UppercaseOutputFile.txt"); cout << "Done! \"UppercaseOutputFile.txt\" open!\n"; //create Upp object to perform an uppercase  //filtration  Upp upperCaseFilter; //perform filtration using the object  cout << "\nNow performing uppercase filtration...\n"; upperCaseFilter.doFilter(inputFile, outputFile); cout << "Filtration done! \"UppercaseOutputFile.txt\""; cout << " is ready!\n"; //output file should now be created  //and written with relevant output  //now close it, because we will open  //another one for the next filter  outputFile.close(); //clear input file flags and move read position  //to the first byte  inputFile.clear(); inputFile.seekg(0L, ios::beg); //open the next output file, for the  //encryption filter  cout << "\nNow opening output file for uppercase filter...\n"; outputFile.open("EncryptionOutputFile.txt"); cout << "Done! \"EncryptionOutputFile.txt\" open!\n"; //create Encrypt object  Encrypt encryptionFilter; //perform file filtration  cout << "\nNow performing encryption filtration...\n"; encryptionFilter.doFilter(inputFile, outputFile); cout << "Filtration done! \"EncryptionOutputFile.txt\""; cout << " is ready!\n"; //output file should now be created  //and written with relevant output  //now close it, because we will open  //another one for the next filter  outputFile.close(); //clear input file flags and move read position  //to the first byte  inputFile.clear(); inputFile.seekg(0L, ios::beg); //open the next output file, for the  //file copy filter  cout << "\nNow opening output file for copy filter...\n"; outputFile.open("CopyOutputFile.txt"); cout << "Done! \"CopyOutputFile.txt\" open!\n"; //create CopyFile object  CopyFile copyFilter; //perform file filtration  cout << "\nNow performing copy filtration...\n"; copyFilter.doFilter(inputFile, outputFile); cout << "Filtration done! \"CopyOutputFile.txt\""; cout << " is ready!\n"; //now close both files  inputFile.close(); outputFile.close(); //return 0 to mark successful termination  return 0; }

RESULT

note: plzzz don't give dislike.....plzzz comment if you have any problem i will try to solve your problem.....plzzz give thumbs up i am in need....


Related Solutions

Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
2. Write a program which reads in the text file generated in part one and writes...
2. Write a program which reads in the text file generated in part one and writes out the same data as a comma-separated values (CSV) file for further processing. The file should have a header line which identifies which column contains which values and should look something like this: Time, Potentiometer, Temperature, Light, Switch0, Switch1, Switch2, Switch3 That header line should be followed by detail lines containing the measurements and should look something like this (matching the above Arduino output):...
Write a program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
Implement in Python a script that does the following: 1) reads input from a supposed file...
Implement in Python a script that does the following: 1) reads input from a supposed file called firstnames_2.txt. 2) processes the input and writes and saves the output to a file. NOTE: Please make sure that the names are written in the outfile with one name on each line no comma ( , ) after the name in the output
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
Write a Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
For example, I have an input text file that reads: "Hello, how are you?" "You look...
For example, I have an input text file that reads: "Hello, how are you?" "You look good today." "Today is a great day." How can I write a function that inputs each line into a node in a linked list? Thank you. (Note: the input text files could have a different amount of lines, and some lines could be blank.) This is the linked list code: #include <iostream> #include <cstdlib> #include <fstream> class Node { public: Node* next; int data;...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT