Question

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

Write a C++ program that reads text from a file and encrypts the file by adding 6 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).

2. Change each character of the string by adding 6 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.

Note that no Raptor program is required for Lab #10. You should, however, think through the structure of the program using a flowchart or pseudo code before starting to write the C++ code. If you want to try this in Raptor be aware that reading and writing files is handled differently in Raptor than in C++.

Once your program executes correctly, upload your .cpp file only. Make sure your name is on the source code in a comment.

Feel free to write a program that decodes this encrypted file for your own amusement.

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'.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

#include <iostream>

#include <fstream>

using namespace std;

int main(int argc, char *argv[])

{

ifstream in("test.txt");

ofstream outfile("encfile.txt");

if(!in) {

cout << "Cannot open input file.\n";

return 1;

}

char str[10000];

char encStr[10000];

while(in) {

in.getline(str, 10000); // delim defaults to '\n'

if(in)

{

int i=0;

cout << str << endl;

for(i=0;str[i]!='\0';i++)

//first we find ascii value by (int)str[i] and then

// adding 4 to it then again converting it to charecter

//by type casting it to (char)

encStr[i] = (char)((int)str[i] + 6);

//add a empty backslash zero to end the charecters array

encStr[i]='\0';

outfile << encStr << '\n';

}

}

OUTPUT :

we read file line by line and encrypted it here is text of file:

х F:\cprogs\filereader.exe This is sample text This is line number second and this is line number third Process exited afterHere is output of encrypted file(Spaces replaced with &):

encfile.txt - Notepad File Edit Format View Help Znoy&oy&ygsvrk&zk-z Znoy&oy&rotk&tshkx&ykiutj gtj&znoy&oy&rotk&t{shkx&znoxj

Here is code for decryption:

#include <iostream>

#include <fstream>

using namespace std;

int main(int argc, char *argv[])

{

ifstream in("encfile.txt");

  

if(!in) {

cout << "Cannot open input file.\n";

return 1;

}

char str[10000];

char decStr[10000];

while(in) {

in.getline(str, 10000); // delim defaults to '\n'

if(in)

{

for(int i=0;str[i]!='\0';i++)

decStr[i] = (char)((int)str[i] - 6);

cout << decStr << endl;

}

  

}

in.close();

return 0;

}

OUTPUT after decrypt:

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that reads text from a file and encrypts the file by adding...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • C++ please Write a program that reads the following sentences from a file: I am Sam...

    C++ please Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with the...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • Write a C program that reads characters from a text file, and recognizes the identifiers in...

    Write a C program that reads characters from a text file, and recognizes the identifiers in the text file. It ignores the rest of the characters. An identifier is a sequence of letters, digits, and underscore characters, where the first character is always a letter or an underscore character. Lower and upper case characters can be part of the identifier. The recognized identifier are copied into the output text. b. Change the C program in exercise 2, so that all...

  • c# csci312: character counter - vector design a program that reads in an ascii text file...

    c# csci312: character counter - vector design a program that reads in an ascii text file (provided) ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provide... C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provided) and creates an output file that contains each unique ASCII...

  • Write a program that reads in a text file, infile.txt, and prints out all the lines...

    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 4 characters. Once it finds a short line (one with fewer than 4 characters), the program stops. For your testing you should create a file named infile.txt. Only upload your Python program, I will create my own infile.txt. Please use a while-loop BUT do not use break, Exit or Quit...

  • Write a program that reads a series of words (one word per line) from a file...

    Write a program that reads a series of words (one word per line) from a file named data.txt. Each word in the file should have each of its characters shifted by 1 character value in the ASCII table (incremented) and then that new word with its characters shifted should be printed to a new file named result.txt. Each word from data.txt should be reprinted with its new encoding in result.txt. Your program should not have any knowledge of how many...

  • C++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design...

    C++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design a hierarchy of Files. You have two different kinds of Files, Text File and an Image File. The files are identified by their name and type, which is identified by either txt or gif extension. An Image file has dimensions of pixel rows and pixel columns. Each pixel has a color depth that can be represented by number of bits. (8 bits is one...

  • Write a C++ program that will read in image data from the file "image.txt" and illustrate...

    Write a C++ program that will read in image data from the file "image.txt" and illustrate that image as ASCII art. The Image file will contain several lines, representing horizontal rows in the image, and each line will have several integers in the range 0-255. representing povels, separated by spaces. You should read this image data into a vector vector in Once you've read in the phel data, go through the image and print out each piel in the image...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT