Encryption program from file stream

First of all Hello !!!
I should write an encryption program which open a text file called input.txt read the content and encrypt it. then save the result in output.txt.here is my code :

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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdio>
using namespace std;
void encrypt(string &input){
	char key;
	int ch;
	ifstream in,read;
	ofstream out,write;
	in.open("key.txt");
	while (in.get(key)){	// encrypting the input string by the algorithm in key file.
		cout<<key;
		if (key=='t'){
			for (int i=0;i<input.length();i++){
				if (input[i]=='t'){
					input[i]='b';				
				}
			}
		}
		else if (key=='c'){
			for (int i=0;i<input.length();i++){
				if (input[i]=='c'){
					input[i]='w';
				}	
			}
		}
		else if (key=='H'){
			for (int i=0;i<input.length();i++){
				if (input[i]=='H'){
					input[i]='m';
				}	
			}
		}
		else if (key==','){
			for (int i=0;i<input.length();i++){
				if (input[i]==','){
					input[i]='?';
				}	
			}
		}
		else if (key=='i'){
			for (int i=0;i<input.length();i++){
				if (input[i]=='i'){
					input[i]='O';
				}	
			}
		}
		
	}
	in.close();
	return;
}
int main()
{
	string choice,text;
	cin>>choice;
	ifstream read;//By  if statements User would choice whether to encrypt or decrypt.
	ofstream write;
	if (choice=="encrypt"){
	read.open("input.txt");
	while (read>>text){
		encrypt(text);
		write.open("output.txt");
		write<<text;
		write.close();
		}
		read.close();
	}
	
}

but I don't know where I am wrong
Last edited on
I can't tell from your question what exactly doesn't work. But one problem I see is your use of ifstream::get in your loop. This function returns EOF once the end of a file has been reaches, but EOF is not guaranteed to be converted to false. You should check for it manually:

1
2
3
4
while(in.get(key) != EOF)
{
    /*Other code*/
}
On the reference page, the return value of get depends on whether the version with or without a parameter is used. Looks like it should be ok as is.
http://www.cplusplus.com/reference/istream/istream/get/
You appear to be right there, I read the reference wrong. The first signature didn't refer to category (1) but to the first signature in category (1).

On closer inspection, you seem to be overwriting your output file on each iteration in your main loop, you use the code:

1
2
3
4
5
6
7
while(read >> text)
{
    encrypt(text);
    write.open("output.txt");
    write << text;
    write.close();
}


By default, ofstream opens in "write" mode, not in "append" mode. So your file will be overwritten each type you open and write something to it, this is every iteration of this loop. You could solve this by opening the file outside of your loop:

1
2
3
4
5
6
7
write.open("output.txt");
while(read >> text)
{
    encrypt(text);
    write << text;
}
write.close();
Topic archived. No new replies allowed.