Keep the content of ifstream and ofstream files unchanged.

How to keep the content of ifstream and ofstream files unchanged.

I want to deal with the following files (all given by the user) or names:

A.txt
A.out
new_A.txt

1-User provides file A.txt to be encrypted.
2-I encrypt it to A.out.
3-If you user asks to decrypt A.out
4-I want to be able to decrypt it in new_A.txt.

However, I initialized all of the 3 streams in the begining and I am able to do only steps 1 and 2. Steps 3 and 4 makes the files empty. Why?

I'm new and I want to know what should be initialized, closed or opened first last etc. Please don't assume that I know certain unwritten rules or laws.

I have the following code (not including extra stuff):
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
	ifstream inputFile;
	ofstream outputEncrypted("A.txt", ios::app);
	ofstream outputDecrypted("new_A.txt", ios::app);
	
	
	while (choice != 'C' || choice != 'c' || choice != 'E' || choice != 'e'){
		
		cout << "\nPlease enter your selection:" << endl;
		cout << "1" << endl;
		cout << "2" << endl;
		cout << "Enter C to continue, or E to exit" << endl;
		cin >>choice;
		
		system("CLS") ;
		if(choice == '1'){
			
			
			cout << "Please enter the file name: ";
			cin >> filename;
			
			inputFile.open(filename.c_str());
			
			if(inputFile.is_open() && outputEncrypted.is_open()){ 
				while(!inputFile.eof()){
					.
					.
					.

					outputEncrypted << temp2 + "\n";			// Output the encrypted messeage line to the file
					
					
				}// End of while-loop
				//cout << "Finished while-loop.\n";
			}
       		else{
        		cout<<"File does not exist!"<< endl;
        		exit(1);
       		}// end of if-else
			
			
			outputEncrypted.close();
			inputFile.close();
			 
		}
		else if (choice == '2'){
			
			// Get the file name to be encrypted.
			cout << "Please enter the file name for encryption: ";
			cin >> filename;
			// Open the file to be encryped.
			inputFile.open(filename.c_str());
			
			if(inputFile.is_open() && outputDecrypted.is_open()){ 
				

				while(!inputFile.eof()){
					.
					.
					.

					outputDecrypted << temp2 + "\n";			// Output the encrypted messeage line to the file
					
				}// End of while-loop
				//cout << "Finished while-loop.\n";
			}
       		else{
        		cout<<"File does not exist!"<< endl;

       		}// end of if-else
			
			inputFile.close();
			
			outputDecrypted.close();
			//exit(1); 



Any Help is greatly appreciated.
Do not try creating the files until the user asks to. More specifically, do not do this:
ChangBroot wrote:
I initialized all of the 3 streams in the begining
Thanks.
Topic archived. No new replies allowed.