Final answer:
The provided C++ program reads characters from an input file, encrypts letters by shifting them forwards three positions in the alphabet, and outputs the encrypted content to another file. Non-alphabetic characters remain unchanged, and the case sensitivity of the letters is preserved.
Step-by-step explanation:
Simple C++ File Encryption Program
The task is to write a C++ program that performs a simple encryption on a text file. This program should read characters from an input file, encrypt alphabetic characters by shifting them three positions forward in the alphabet (with wrap-around), and write the result to an output file. Non-alphabetic characters will be written to the output file as is, and the case of the letters will be preserved.
Here is an example of a C++ program that can accomplish this:
#include
#include
#include
using namespace std;
int main() {
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");
if(!inputFile.is_open() || !outputFile.is_open()) {
cerr << "Could not open files!" << endl;
return 1;
}
char ch;
while(inputFile.get(ch)) {
if(isalpha(ch)) {
char base = isupper(ch) ? 'A' : 'a';
ch = (ch - base + 3) % 26 + base;
}
outputFile << ch;
}
inputFile.close();
outputFile.close();
cout << "Encryption complete." << endl;
return 0;
}
Make sure to handle the file stream exceptions appropriately, and replace "input.txt" and "output.txt" with the actual file names you want to work with.