Encrypting contents from file and saving to another file

Hello everyone,

Right now I'm working on an assignment where I need to take the data from on file encrypt it by adding 5 to each byte and then save it to a user specified location. I think I'm close to having it done but I've run into a hick up. After I get the user input for where the encrypted data should be saved the program seems to never end. This is the code I have so far and any type of help is appreciated greatly.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

ifstream input;
ofstream output;
string inputfile, outputfile;

cout << "Enter input file name: ";
cin >> inputfile;

input.open(inputfile);

cout << "Enter output file name: ";
cin >> outputfile;

output.open(outputfile);

char ch = input.get();
while(!input.eof()){
int temp = (sizeof(ch) + 5);
output.put(temp);
ch = input.get();
}

input.close();
output.close();

return 0;

}
Last edited on
I ran this code on my compiler and it ran fine. I'm thinking you input the file location wrong. Try adding this code after opening the file. If you get the error message then you know what your problem is.
1
2
3
4
5
6
if (input.fail())
	{
		cout << "The file failed to open" << endl;
		cin.ignore();
		exit(0);
	}



oh and please use code tags when you post. There is a little menu on the right side of the screen when you post. The code tags button looks like this <>.
Topic archived. No new replies allowed.