Write a C++ program that reads text from a file and encrypts the file by adding
4 to the ASCII value of each character.
Your program should:
1. Read the provided plain.txt file one line at a time. Because this file has
spaces, use getline (see section 3.8).
2. Change each character of the string by adding 4 to it.
3. Write the encoded string to a second file, such as coded.txt
4. The encoded file should have the same structure as the original file, if the
second line of the original file has 24 characters (including spaces) then
there should be 24 characters on the second line of the encoded file
(spaces will now look like '$').
Hints:
1. In Visual Studio Express, put your plain.txt file in the project directory. The
same folder that your C++ source (.cpp) file is in.
2. One of the challenges is to keep reading lines from the plain.txt file until
the end. Sections 12.4 and 12.5 describe how you can do that
Okay so the only problem that I have is trying to figure out how to write the encrypted code to the new file...please help with an explanation would be extremely helpful!!
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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string input;
fstream inputFile;
fstream outFile;
//Open the file for input mode
inputFile.open("plain.txt", ios::in);
// If the file opened successfully
if (inputFile)
{
while (inputFile)
{
cout << input << endl;
getline(inputFile, input);
for (int i = 0; i < input.size(); i++)
{
input[i] += 4;
}
}
inputFile.close();
}
else
{
cout << "Error: Unable to open file." << endl;
}
system("pause");
return 0;
}
|