Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that reads text from a file and encrypts the file by adding

ID: 3575604 • Letter: W

Question

Write a C++ program that reads text from a file and encrypts the file by adding 4 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files.

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).

Use the following as the plain.txt file:

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'.

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 '$').

Explanation / Answer

#include <iostream>
#include<fstream>
using namespace std;

int main()
{
char data[100];
// open a file in read mode.
ifstream infile;
infile.open("plain.txt");

// open a file in write mode.
ofstream outfile;
outfile.open("coded.txt");

cout << "Reading from the file" << endl;
infile.getline(data, 100);

for(int i=0; data[i]!=''; i++)
{

data[i]=(int)(data[i]+4);

}
cout << "Writting from the file" << endl;
outfile<<data;

// close the opened files.
infile.close();
outfile.close();
return 0;
}


plain.txt
This is a sample input file for COSC 1436
coded.txt
Xlmw$mw$e$weqtpi$mrtyx$jmpi$jsv$GSWG$587:

OUTPUT:

Reading from the file
Writting from the file

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote