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):
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();
}
elseif (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);