File Filter Assignment

I need help with this assignment. I know pure basics cin, cout, type stuff. I don't know this advanced stuff.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  #include <iostream>
#include <fstream>
#include <cctype>
using namespace std;

//  This is the abstract Filter class. Concrete derived classes will override the //  transform member function class Filter


{
public:
    void doFilter(WHAT SHOULD YOU PUT IN HERE);
protected:
    virtual char transform(char ch) = 0;
};


//   Filter::doFilter  Performs actual filtering.

void Filter::doFilter(WHAT SHOULD YOU PUT IN HERE)


// Uppercase subclass filter class


int main( )

// Explain the program


// Echo the input file

// Get name of output file

// Open the output file

// Process

// Close output file and reopen for  reading so we can  print

// Close all files


//   Print file

// Print the file contents

// Go back to beginning of file for further processing


can you make a simple class yet?
can you read a file yet?
you have to start with the small pieces and build up...

an easy encryption algorithm:
srand(number_password);
for(all the bytes)
byte ^= rand();

decryption is the exact same algorithm. If you bungle the password, you get junk out, if you get it right, it decrypts to the original. the output will be binary, not text, so watch that.

upper case of a file can be done by loading the entire file into one giant string and doing a std::transform of toupper on it.

so those 2 tasks can be done with minimal code (about 5 lines each give or take), you just need to wrap the class/virutal concepts around them. I would first code these 3 things (read a file and write it back out doing nothing), then uppercase, and finally encryption (be sure to test decrypting it back to the original). Once you have these stub pieces working, then wrap the class stuff around those ideas.

Last edited on
Hi Jonnin,

No, I don't know how to do classes and read a file yet. Ive been trying to read and learn on tutorial sites but still dont understand and my homework assignment is due relatively soon.
Cross posted in DreamInCode: https://www.dreamincode.net/forums/topic/418484-file-filter/

Hopefully Handy Andy can save your bacon again.
This should be enough to get you started. Study, learn, and tinker with the code. But you won't learn if someone just does it all for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;


//First declare your base class
class baseFilterClass
{
public:
	void doFilter(ifstream& in, ofstream& out) {
		string s;
		
		while (getline(in, s, '\0')) {
			for (char mychar : s) {
				mychar = transform(mychar);
				out << mychar;
			}
		}
	}
protected:
	virtual char transform(char ch) = 0; //This is a pure virtual function declaration
};


//A derived class to make a file uppercase
class derivedUppercaseClass: public baseFilterClass
{
	public:
		char transform(char ch) {
			return toupper(ch);
		}
};

int main() { 
	
	ifstream myInfile;
	ofstream myOutfile;

	myInfile.open("infile.txt");
	myOutfile.open("outfile.txt");
	
	derivedUppercaseClass myClass;
	myClass.doFilter(myInfile, myOutfile);

	myInfile.close();
	myOutfile.close();

	return 0;
}
Last edited on
can someone help me debug. im getting an error at line 10 and 20 but don't know what i need to do or fix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//Charles Blackwell CIS221 M6
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;


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


//   Filter::doFilter  Performs actual filtering.

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


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

void printFile(fstream &);

const int size_file = 91;


// Explain the program

int main() {
    char inFileName[size_file];
    char outFileName[size_file];

    cout << "This program creates the upper case version of the file" << endl;
    cout << "Now enter the name of the input file (input.txt): " << endl;

    cin >> inFileName;

    fstream inputFile(inFileName, ios::in);
    if (!inputFile)
    {
        cout << "File " << inFileName << " will not open. " << endl;
        EXIT(EXIT_FAILURE)
    }

    // Echo the input file and get name of output file

    cout << "\nOriginal file is: " << endl;

    printFile(inputFile);
    cout << endl;

    cout << "Enter the name of the output file: " << endl;
    cin >> outFileName;

    fstream outputFile(outFileName, ios::out);
    if (!outputFile)
    {
        cout << "File " << inFileName << " will not open. " << endl;
        EXIT(EXIT_FAILURE);
    }

    //process

    UpperCaseFilter upperFilter;
    upperFilter.doFilter(inputFile, outputFile);

    //close output file

    outputFile.close();
    outputFile.open(outFileName, ios::in);
    cout << "\nFiltered file is: " << endl;
    printFile(outputFile);

    inputFile.close();
    outputFile.close();
    return 0;
}

void printFile(fstream &file)
{
    file.clear();
    file.seekg(0; ios::beg);

    char ch = file.get();
    while (ch != EOF)
    {
        cout << ch << endl;
        ch = file.get();
    }

    file.clear();
    file.seekg(0, ios::beg);




}
Here you go friend. Glad to see you're trying.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//Charles Blackwell CIS221 M6
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;

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


//   Filter::doFilter  Performs actual filtering.

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


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

void printFile(fstream&);

const int size_file = 91;


// Explain the program

int main() {
	char inFileName[size_file];
	char outFileName[size_file];

	cout << "This program creates the upper case version of the file" << endl;
	cout << "Now enter the name of the input file (input.txt): " << endl;

	cin >> inFileName;

	fstream inputFile(inFileName, ios::in);
	if (!inputFile)
	{
		cout << "File " << inFileName << " will not open. " << endl;
		return(EXIT_FAILURE);
	}

	// Echo the input file and get name of output file

	cout << "\nOriginal file is: " << endl;

	printFile(inputFile);
	cout << endl;

	cout << "Enter the name of the output file: " << endl;
	cin >> outFileName;

	fstream outputFile(outFileName, ios::out);
	if (!outputFile)
	{
		cout << "File " << inFileName << " will not open. " << endl;
		return(EXIT_FAILURE);
	}

	//process

	UpperCaseFilter upperFilter;
	upperFilter.doFilter(inputFile, outputFile);

	//close output file

	outputFile.close();
	outputFile.open(outFileName, ios::in);
	cout << "\nFiltered file is: " << endl;
	printFile(outputFile);

	inputFile.close();
	outputFile.close();
	return 0;
}

void printFile(fstream& file)
{
	file.clear();
	file.seekg(0, ios::beg);

	char ch = file.get();
	while (ch != EOF)
	{
		cout << ch;
		ch = file.get();
	}

	file.clear();
	file.seekg(0, ios::beg);




}
Last edited on
a class is a user defined variable type.
you can't just say x = 3 without making x have a type, and same for your own types, so you have to name it. the above names it by saying class filter; yours left the name and class description off so it would not compile there.

then you can use it:
filter x; //filter type created and exists, ok, can make variable of that type now.
Thank you Jonnin and Manga for you assistance
Topic archived. No new replies allowed.