// // encrypt1.cpp // CSC 215 // John Rogers // // This program encrypts the input // in a text file selected by the // user and places the output in // a text file selected by the user. // It does this by performing // character input and output. // #include #include #include #include void openFiles(ifstream& inputFile, ofstream& outputFile); char encrypt(char clearChar, char key); int main() { ifstream inputFile; ofstream outputFile; char clearChar, encryptedChar, key; openFiles(inputFile, outputFile); cout << "Please enter the key character: "; cin >> key; inputFile.get(clearChar); while (! inputFile.eof()) { encryptedChar = encrypt(clearChar, key); outputFile.put(encryptedChar); inputFile.get(clearChar); } inputFile.close(); outputFile.close(); return 0; } // // // This function prompts the user for and opens // two files: one for input and one for output. // The first is connected to an input file stream // object and the second to an output file stream // object, both of which are passed as call-by- // reference parameters. // void openFiles(ifstream& inputFile, ofstream& outputFile) { char inputFileName[50], outputFileName[50]; do { cout << "Please enter the name of the input file: "; cin >> inputFileName; inputFile.open(inputFileName); if (inputFile.fail()) { cout << "Cannot open the file: " << inputFileName << endl; } } while (inputFile.fail()); do { cout << "Please enter the name of the output file: "; cin >> outputFileName; outputFile.open(outputFileName); if (outputFile.fail()) { cout << "Cannot open the file: " << outputFileName << endl; } } while (outputFile.fail()); return; } // // // This function uses an exclusive-or // to encrypt a character. Note that // _all_ characters get encrypted. // char encrypt(char clearChar, char key) { char encryptedChar; encryptedChar = clearChar ^ key; return encryptedChar; }