Question

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 General, for the Nth line, the encryption factor is (N -1) % 4 + 1.

If the seventh line of the input file is, “This is the seventh line of the file.” The seventh line of the encrypted file would have 3 added to each character. It would look like, “Wklv#lv#wkh#vhyhqwk#olqh#ri#wkh#iloh1”

See section 5.11 and sections 12.3-5 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 the encryption factor 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 be replaced by the characters !, ”, #, and $).

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. Program 12-8 is an example of how to do that.

plain.txt:

This is a sample input file for COSC 1436
assignment #10. Your program should encode this
file by adding 1 through 4 to the ASCII value of each
character in it. In your encoded file the first character should
be 'U'. The last character of the third line should be 'k'.
The encoded file should have the same number of lines as the input,
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

Whether you face any problem or need any modification then share with me in the comment section, I'll happy to help you.

Program:

// encryption.cpp

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>

using namespace std;


//main function
int main()
{
   ifstream fin;
   //open the input file
   fin.open("plain.txt");
   //check file exist or not
   if(!fin.is_open())
   {
       cerr<<"File not exist! " <<endl;
       return 1;
   }
   ofstream fout;
   //open the output file
   fout.open("coded.txt");

   string line;
   int encryption_factor, N = 1;

   //processing
   while(1)
   {
       //calculate encryption_factor
       encryption_factor = (N -1) % 4 + 1;
       //read a line from the file
       getline(fin, line, '\n');
       //check for end-of-file
       if(fin.eof()) break;
       //encrypt the line using encryption_factor
       for(int i=0; i<line.size(); i++)
       {
           line[i] = line[i] + encryption_factor;
       }
       //write to the file
       fout << line <<endl;
       //increase the line number
       N++;
   }
   //close the files
   fin.close();
   fout.close();

   return 0;
}

Output File: coded.txt

Uijt!jt!b!tbnqmf!joqvu!gjmf!gps!DPTD!2547
cuukipogpv"%320"[qwt"rtqitco"ujqwnf"gpeqfg"vjku
iloh#e|#dgglqj#4#wkurxjk#7#wr#wkh#DVFLL#ydoxh#ri#hdfk
glevegxiv$mr$mx2$Mr$}syv$irgshih$jmpi$xli$jmvwx$glevegxiv$wlsyph
cf!(V(/!Uif!mbtu!dibsbdufs!pg!uif!uijse!mjof!tipvme!cf!(l(/
Vjg"gpeqfgf"hkng"ujqwnf"jcxg"vjg"ucog"pwodgt"qh"nkpgu"cu"vjg"kprwv.
exw#zloo#eh#xquhdgdeoh1#Wkh#odvw#fkdudfwhu#ri#wkh#odvw#olqh#zloo
fi$e$+6+2

Add a comment
Know the answer?
Add Answer to:
Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...
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
  • 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...

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

  • In this lab, you will write a C program to encrypt a file. The program prompts...

    In this lab, you will write a C program to encrypt a file. The program prompts the user to enter a key (maximum of 5 bytes), then the program uses the key to encrypt the file. Note that the user might enter more than 5 characters, but only the first 5 are used. If a new line before the five characters, the key is padded with 'a'; Note that the new line is not a part of the key, but...

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

  • Write a new program (hw6-pr1) meeting at least the following minimum requirements: Opens the text version...

    Write a new program (hw6-pr1) meeting at least the following minimum requirements: Opens the text version of this file (hw6-Spring2017.txt) and searches thru the file for the first occurrence of each of the 26 uppercase characters of the alphabet (A-Z), then each the 10 digits (0-9), and finally each of the 26 lowercase characters of the alphabet (a-z). As it finds each of these characters it should also record its position in the file (assume the first character in the...

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

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

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

  • 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]...

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