6 Replies - 4320 Views - Last Post: 03 November 2012 - 09:20 AM Rate Topic: -----

#1 playthalius  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 26-September 12

File Filter Problems with encryption key and member function.

Posted 31 October 2012 - 10:46 AM

i am supposed to write a program that opens a text document and have 3 subclasses transform the data from the document and write them to 3 diferent text files. The first one is supposed to do an encryption, the second is supposed to tranform all the data to uppercase, and the thrid is supposed to just copy the original data. I wrote the program and since I dont have a book I didnt know all the instructions. I got the program running just fine but when I showed it to him he pointed out that I am supposed to a have user enter an encryption key to encrypt the first set of data instead of an embedded encryption.

I am also supposed to include a member function:

void doFilter(ifstream &in, ofstream &out)

When he brought these up to me I hit a brick wall and I am having problems rewriting program to take these changes.

Here is my main cpp file
#include"Filter.h"
#include<iostream>
#include<fstream>

using namespace std;

int main()
{
	// declare local variables
	const int NAME_LENGTH = 80;
	char inFileName[NAME_LENGTH], outFileName[NAME_LENGTH];

	// get the file to open and encrypt
	cout << "\n\n\n";
	cout << " Enter the file name: ";
	cin >> inFileName;

	// enter the file to write the encrypted data to
	cout << "\n\n";
	cout << " Enter the file to hold the Encrypted data: ";
	cin >> outFileName;

	// encrypt the data
	Encryption fileEncrypt(inFileName, outFileName);
	fileEncrypt.encrypt();

	// enter the file to write the uppercase transformation to
	cout << "\n\n";
	cout << " Enter the file name to hold the uppercase data: ";
	cin >> outFileName;

	// transform file to uppercase
	Upper fileUpper(inFileName, outFileName);
	fileUpper.encrypt();

	// enter the file to write the original data to
	cout << "\n\n";
	cout << " Enter the file to hold the original data: ";
	cin >> outFileName;

	// copy original data
	Original fileOriginal(inFileName, outFileName);
	fileOriginal.encrypt();

	return 0;
}





Here is my class header file.
#include<iostream>
#include<fstream>

using namespace std;

// main class file
 class Filter
 {
 protected:
	 ifstream inFile;
	 ofstream outFile;
 public:
	Filter(char *inFileName, char *outFileName); // constructor	
	~Filter(); // destructor

	virtual char transform(char ch) = 0; // pure virtual function
	void encrypt();
 
 };

 // constructor
 Filter::Filter(char *inFileName, char *outFileName)
 {
	 // open the file to recieve the data from and send the data to
	inFile.open(inFileName);
	outFile.open(outFileName);
	// validate the file exists
	if(!inFile)
	{
		cout << "\n\n";
		cout << " The file " << inFileName << " cannot be opened!! \n\n";
		exit(1);
	}
	// validates the fiel can be written
	if(!outFile)
	{
		cout << "\n\n";
		cout << " The file " << outFileName << " cannot be opened!! \n\n";
		exit(1);
	}
 
 }

 // desturctor
 Filter::~Filter()
 {
	 // close the files opened and created
	inFile.close();
	outFile.close();
 }

 // encryption function
 void Filter::encrypt()
 {
	 // local variables
	char ch;
	char transCh;
	// get the data from the file
	inFile.get(ch);
	// get the data until you reach the end
	while(!inFile.fail())
	{
		// sets transCh to the returned value of the virtual function
		transCh = transform(ch);
		// writes the data to the created file as it is transformed
		outFile.put(transCh);
		// gets the next character 
		inFile.get(ch);
	}
 }

 // sub class to encrypt the data
 class Encryption:public Filter
 {
 public:
	 	 
	 // virtual function and returns encrypted data
	 char transform(char ch)
	 {
		return ch + 1;
	 }

	 Encryption(char *inFileName, char *outFileName):Filter(inFileName, outFileName)
	 {}
 };

 // sub class to tranform data to all uppercase
 class Upper:public Filter
 {
 public:
	 // virtual that transforms the data to all uppercase
	 char transform(char ch)
	 {
		return toupper(ch);
	 }

	 Upper(char *inFileName, char *outFileName):Filter(inFileName, outFileName)
	 {}
 };

 // sub class to copy original data
 class Original:public Filter
 {
 public:
	 // virtual function to return original data
	 char transform(char ch)
	 {
		return ch;
	 }
	 Original(char *inFileName, char * outFileName):Filter(inFileName, outFileName)
	 {}
 };



I am confused cause it looks like its gonna change every part of my code just slightly but I dont even know where to begin.

Is This A Good Question/Topic? 0
  • +

Replies To: File Filter Problems with encryption key and member function.

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: File Filter Problems with encryption key and member function.

Posted 31 October 2012 - 04:31 PM

Hi, the doFilter function will be a member of the Filter class, and it will call the transform function.
Was This Post Helpful? 0
  • +
  • -

#3 playthalius  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 26-September 12

Re: File Filter Problems with encryption key and member function.

Posted 01 November 2012 - 10:36 AM

I just started from scrat5ch and rebuilt the program. I figured it wouold be easier than trying to work the original one around the new problem.

Now I am having problems with the transformations. the encryption comes out just fine. its the upper case and original copied ones that arent doing anything. I have feeling tis something easy that I am just overlooking or just have the wrong understanding of. The upper and original jsut post blank text documents after the program runs.

#include"Filter.h"
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
	// local variables, fstrwam objects, and class objects
	ifstream input;
	ofstream encrypt, upper, original;
	Upper up;
	Original or;
	int encryptKey;

	// open the text file with the data to be transformed
	input.open("file.txt", ios::in);
	if(input.fail()) // verifies file can be opened
	{
		cout << "\n\n";
		cout << " Input file cannot be opened!!";
	}
	// opens the file to write the encrypted data to
	encrypt.open("encrypt.txt", ios::out);
	if(encrypt.fail()) // verifies the file can be opened
	{
		cout << "\n\n";
		cout << " Encrypted file cannot be opened!!";
	}
	// opens the file to write the upper case transformed data to
	upper.open("upper.txt", ios::out);
	if(upper.fail()) // verifies the file can be opened
	{
		cout << "\n\n";
		cout << " Upper case file cannot be opened";
	}
	// openes the file that the original data will be copied to
	original.open("original.txt", ios::out);
	if(original.fail()) // verifies the file can be opened
	{
		cout << "\n\n";
		cout << " The original copied file cannot be opened!!";
	}

	// gets teh encryption key value from user
	cout << "\n\n";
	cout << " Enter the encryption value: ";
	cin >> encryptKey;
	// this is where it calls the class objects using the fstream objects as parameters
	Encryption encryptFile(encryptKey);
	encryptFile.doFilter(input, encrypt); // the encryption file come sout just fine
	Upper upperFile(); // this one does nothing that i can see. just a blank text document
	upperFile.doFilter(input, upper); 
	or.doFilter(input, original); // the original copied one does the same as the upper case.

	// close all open files
	input.close(); 
	encrypt.close();
	upper.close();
	original.close();

	return 0;
}


#include<iostream>
#include<fstream>

using namespace std;

// main class
class Filter
{
public:
	Filter();
	~Filter();
	virtual char transform(char ch) = 0;
	void doFilter(ifstream &in, ofstream &out);
};
// default constructor
Filter::Filter()
{

}
// destructor
Filter::~Filter()
{

}
// the member function that calls the virtual function
void Filter::doFilter(ifstream &in, ofstream &out)
{
	char ch;
	char transCh;

	in.get(ch);

	while(!in.fail())
	{
		// sets transCh to the returned value of the virtual function
		transCh = transform(ch);
		// writes the data to the created file as it is transformed
		out.put(transCh);
		// gets the next character 
		in.get(ch);
	}
}
// sub class to encrypt the data
class Encryption: public Filter
{
private:
	int key;
public:
	Encryption(int k);
	~Encryption();
	char transform(char ch);
};

Encryption::Encryption(int k) { key = k; }
Encryption::~Encryption(){}
// sub class encrytpion virtual function transform definition
char Encryption::transform(char ch)
{ 
	return ch + key; 
}
// sub class upper that transforms all data to uppercase
class Upper: public Filter
{
public:
	Upper();
	~Upper();
	char transform(char ch);
};

Upper::Upper(){}
Upper::~Upper(){}
// sub class uppers virtual functions transform definition
char Upper::transform(char ch)
{ 
	return toupper(ch); 
}
// sub class original that copies the original data 
class Original: public Filter
{
public:
	Original();
	~Original();
	char transform(char ch);
};

Original::Original(){}
Original::~Original(){}
// sub class original virtual function transform definition
char Original::transform(char ch)
{ 
	return ch; 
}

This post has been edited by jimblumberg: 01 November 2012 - 11:38 AM
Reason for edit:: Fixed Code tags.

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: File Filter Problems with encryption key and member function.

Posted 01 November 2012 - 03:15 PM

When the input file is first used it runs to the end of the file. For the other filters it is not reset.
Was This Post Helpful? 0
  • +
  • -

#5 playthalius  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 26-September 12

Re: File Filter Problems with encryption key and member function.

Posted 03 November 2012 - 08:04 AM

First off thanks for pointing that out for me. Its something simpole but I havent had to consider before.

Ok so do i need to put a
in.seekg(0, ios::beg)



in each subclass virtual. or in the doFilter member function. I am going to try it in both but if Im way off track tell me Im wrong so I can look into other things.
Was This Post Helpful? 0
  • +
  • -

#6 playthalius  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 26-September 12

Re: File Filter Problems with encryption key and member function.

Posted 03 November 2012 - 08:18 AM

I dont get it. It should be setting the flag back to the beginning of the input file everytime the doFilter member function is called now. and sicne the doFilter is called everytime to do the transformations it should reset the position of the file to the beginning. unless I have the syntax wrong. Im gonna post my code again. I went ahead and put a out.clear() in there to just incase it thinks something was already in the output files.

#include"Filter.h"
#include<iostream>
#include<fstream>
#include<string>

using namespace std;


int main()
{
	// local variables, fstrwam objects, and class objects
	ifstream input;
	ofstream encrypt, upper, original;
	Upper up;
	Original or;
	int encryptKey;

	// open the text file with the data to be transformed
	input.open("file.txt", ios::in);
	if(input.fail()) // verifies file can be opened
	{
		cout << "\n\n";
		cout << " Input file cannot be opened!!";
	}
	// opens the file to write the encrypted data to
	encrypt.open("encrypt.txt", ios::out | ios::trunc);
	if(encrypt.fail()) // verifies the file can be opened
	{
		cout << "\n\n";
		cout << " Encrypted file cannot be opened!!";
	}
	// opens the file to write the upper case transformed data to
	upper.open("upper.txt", ios::out | ios::trunc);
	if(upper.fail()) // verifies the file can be opened
	{
		cout << "\n\n";
		cout << " Upper case file cannot be opened";
	}
	// openes the file that the original data will be copied to
	original.open("original.txt", ios::out | ios::trunc);
	if(original.fail()) // verifies the file can be opened
	{
		cout << "\n\n";
		cout << " The original copied file cannot be opened!!";
	}

	// gets teh encryption key value from user
	cout << "\n\n";
	cout << " Enter the encryption value: ";
	cin >> encryptKey;
	// this is where it calls the class objects using the fstream objects as parameters
	Encryption encryptFile(encryptKey);
	encryptFile.doFilter(input, encrypt); // the encryption file come sout just fine
	 // this one does nothing that i can see. just a blank text document
	up.doFilter(input, upper); 
	or.doFilter(input, original); // the original copied one does the same as the upper case.	
	// close all open files
	input.close(); 
	encrypt.close();
	upper.close();
	original.close();

	return 0;
}





#include<iostream>
#include<fstream>

using namespace std;

// main class
class Filter
{
public:
	Filter();
	~Filter();
	virtual char transform(char ch) = 0;
	void doFilter(ifstream &in, ofstream &out);
};
// default constructor
Filter::Filter()
{

}
// destructor
Filter::~Filter()
{

}
// the member function that calls the virtual function
void Filter::doFilter(ifstream &in, ofstream &out)
{
	char ch;
	char transCh;
	out.clear();
	in.seekg(0L, ios::beg); // here is where i set the flags.
	in.get(ch);

	while(!in.fail())
	{
		// sets transCh to the returned value of the virtual function
		transCh = transform(ch);
		// writes the data to the created file as it is transformed
		out.put(transCh);
		// gets the next character 
		in.get(ch);
	}
}
// sub class to encrypt the data
class Encryption: public Filter
{
private:
	int key;
public:
	Encryption(int k);
	~Encryption();
	char transform(char ch);
};

Encryption::Encryption(int k) { key = k; }
Encryption::~Encryption(){}
// sub class encrytpion virtual function transform definition
char Encryption::transform(char ch)
{ 
	return ch + key; 
}
// sub class upper that transforms all data to uppercase
class Upper: public Filter
{
public:
	Upper();
	~Upper();
	char transform(char ch);
};

Upper::Upper(){}
Upper::~Upper(){}
// sub class uppers virtual functions transform definition
char Upper::transform(char ch)
{ 
	return toupper(ch); 
}
// sub class original that copies the original data 
class Original: public Filter
{
public:
	Original();
	~Original();
	char transform(char ch);
};

Original::Original(){}
Original::~Original(){}
// sub class original virtual function transform definition
char Original::transform(char ch)
{ 
	return ch; 
}



I guess after loooking at it it wouldnt worked in the virtual function lol. But its still doing the same thing. Encrytping the first but noting on the other two.

This post has been edited by playthalius: 03 November 2012 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#7 playthalius  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 26-September 12

Re: File Filter Problems with encryption key and member function.

Posted 03 November 2012 - 09:20 AM

Ok i got it working right. The one thing i didnt think of but I needed to clear the flags for the in file as well as the out file. I went ahead and dynamically created the files so the user can input his own file. Here is the updated code

#include"Filter.h"
#include<iostream>
#include<fstream>
#include<string>

using namespace std;


int main()
{
	// local variables, fstrwam objects, and class objects
	ifstream input;
	ofstream encrypt, upper, original;
	Upper up;
	Original or;
	int encryptKey;
	string inFile, encryptFile, upperFile, originalFile;


	cout << "\n\n";
	cout << " Enter the file to transform: ";
	cin >> inFile;
	// open the text file with the data to be transformed
	input.open(inFile, ios::in);
	while(input.fail()) // verifies file can be opened
	{
		cout << "\n\n";
		cout << " Input file cannot be opened!!";
		cout << "\n\n";
		cout << " Re-enter file name: ";
		cin >> inFile;
		input.open(inFile, ios::in);
	}

	cout << "\n\n";
	cout << " Enter the file to write the encrypted data to: ";
	cin >> encryptFile;
	// opens the file to write the encrypted data to
	encrypt.open(encryptFile, ios::out | ios::trunc);
	if(encrypt.fail()) // verifies the file can be opened
	{
		cout << "\n\n";
		cout << " Encrypted file cannot be opened!!";
	}

	cout << "\n\n";
	cout << " Enter the file to write the upper case transformation to: ";
	cin >> upperFile;
	// opens the file to write the upper case transformed data to
	upper.open(upperFile, ios::out | ios::trunc);
	if(upper.fail()) // verifies the file can be opened
	{
		cout << "\n\n";
		cout << " Upper case file cannot be opened";
	}

	cout << "\n\n";
	cout << " Enter the file to write a copy of the original data to: ";
	cin >> originalFile;
	// openes the file that the original data will be copied to
	original.open(originalFile, ios::out | ios::trunc);
	if(original.fail()) // verifies the file can be opened
	{
		cout << "\n\n";
		cout << " The original copied file cannot be opened!!";
	}

	// gets teh encryption key value from user
	cout << "\n\n";
	cout << " Enter the encryption value: ";
	cin >> encryptKey;
	// this is where it calls the class objects using the fstream objects as parameters
	Encryption encryptedFile(encryptKey);
	encryptedFile.doFilter(input, encrypt); // the encryption file come sout just fine
	 // this one does nothing that i can see. just a blank text document
	up.doFilter(input, upper); 
	or.doFilter(input, original); // the original copied one does the same as the upper case.	
	// close all open files
	input.close(); 
	encrypt.close();
	upper.close();
	original.close();

	cout << "\n\n\n\n\n";
	return 0;
}





#include<iostream>
#include<fstream>

using namespace std;

// main class
class Filter
{
public:
	Filter();
	~Filter();
	virtual char transform(char ch) = 0;
	void doFilter(ifstream &in, ofstream &out);
};
// default constructor
Filter::Filter()
{

}
// destructor
Filter::~Filter()
{

}
// the member function that calls the virtual function
void Filter::doFilter(ifstream &in, ofstream &out)
{
	char ch;
	char transCh;
	out.clear();
	in.clear();
	in.seekg(0L, ios::beg);
	in.get(ch);

	while(!in.fail())
	{
		// sets transCh to the returned value of the virtual function
		transCh = transform(ch);
		// writes the data to the created file as it is transformed
		out.put(transCh);
		// gets the next character 
		in.get(ch);
	}
}



// sub class to encrypt the data
class Encryption: public Filter
{
private:
	int key;
public:
	Encryption(int k);
	~Encryption();
	char transform(char ch);
};

Encryption::Encryption(int k) { key = k; }
Encryption::~Encryption(){}
// sub class encrytpion virtual function transform definition
char Encryption::transform(char ch)
{ 
	return ch + key; 
}



// sub class upper that transforms all data to uppercase
class Upper: public Filter
{
public:
	Upper();
	~Upper();
	char transform(char ch);
};

Upper::Upper(){}
Upper::~Upper(){}
// sub class uppers virtual functions transform definition
char Upper::transform(char ch)
{ 
	return toupper(ch); 
}




// sub class original that copies the original data 
class Original: public Filter
{
public:
	Original();
	~Original();
	char transform(char ch);
};

Original::Original(){}
Original::~Original(){}
// sub class original virtual function transform definition
char Original::transform(char ch)
{ 
	return ch; 
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1