0% found this document useful (0 votes)
219 views

A File Filter Reads An Input File

The document describes an abstract FileFilter class with a pure virtual transform function to define different types of file filters. Three derived classes are defined - one to transform text to uppercase, one for encryption with a key, and one to make an unchanged copy. The base FileFilter class has a doFilter function to perform filtering, which calls the transform function on each character read from the input file and writes it to the output file.

Uploaded by

VIKRAM KUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
219 views

A File Filter Reads An Input File

The document describes an abstract FileFilter class with a pure virtual transform function to define different types of file filters. Three derived classes are defined - one to transform text to uppercase, one for encryption with a key, and one to make an unchanged copy. The base FileFilter class has a doFilter function to perform filtering, which calls the transform function on each character read from the input file and writes it to the output file.

Uploaded by

VIKRAM KUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

A file filter reads an input file, transforms it in some way, and writes the results to an out put 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, ofstream &out) This function
should be 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.

Answer

//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

2
//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

3
//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

4
//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

5
//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;

You might also like