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 '$').
The plain.txt file contains this:
This is a sample input file for COSC 1436
assignment #10. Your program should encode this
file by adding four to the AsCII value of each
character in it. In your encoded file the first character should
be 'X'. The last character of the first line should be ':'.
The encoded file should have 8 lines of text just like this one,
but will be unreadable. The last character of the last line will
be a '2'.
The main problem I'm having with this program is saving the string to the outfile correctly. The text displays correctly in the cmd like plain.txt says it should but when I save string plain; to coded.txt it just comes out with jm(i(/:/6
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
|
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
string plain;
infile.open("plain.txt", ios::in);
if (infile)
{
while (infile)
{
cout << plain << endl;
getline(infile, plain);
for (int index = 0; index < plain.size(); index++)
{
plain[index] += 4;
}
}
infile.close();
}
else
{
cout << "Input file open failed!" << endl;
}
outfile.open("coded.txt");
if (outfile)
{
outfile << plain << endl;
}
else
{
cout << "Output file open failed!" << endl;
}
outfile.close();
system("pause");
}
|