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

C++ Programming Consider the following scheme for encrypting a text file. The or

ID: 3872086 • Letter: C

Question

C++ Programming   

Consider the following scheme for encrypting a text file. The original file is split into two new files. One of the two new files will contain all the characters in the original file in odd position and the other new file will contain all the characters in the original file in even position. That is, the first file would contain the first, third, fifth, seventh, etc. characters from the original and the other file would contain the second, fourth, sixth, etc. characters. For example, if the original file contained the text: That's all folks! then the first file would contain: Ta' l ok! and the second file would contain: htsalfls

For this assignment you are to implement the encryption and decryption functions for such a scheme and write a driver program to test them.

The first function

                  void split(char sourceFile[], char destFile1[], char destFile2[]);

should:

1.     Open sourceFile as an input file stream,

2.     Open destFile1 and destFile2 as output file streams

3.     Read each character from sourceFile and

4.     If it's an even numbered character (starting at 0), place it in destFile1

5.     Else place it in destFile2

6.     Close the files

The second function

void merge(char sourceFile1[], char sourceFile2[], char destFile[]);

should reverse the previous process.

To test these two functions, your main function should do the following:

1)    Ask the user for the name of the input file.

2)    Ask the user for the names of the two half files.

3)    Call the split function with the filenames as parameters.

4)    Ask the user for the name for the recombined file.

5)    Call the merge function with the appropriate filenames as parameters.

Your program should also:

Give detailed directions and warnings to the user.

Be readable with appropriate documentation and formatting.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Function prototypes
void split(char sourceFile[], char destFile1[], char destFile2[]);
void merge(char sourceFile1[], char sourceFile2[], char destFile[]);

//Main function
int main()
{
    char source[20], dest1[20], dest2[20], destination[20];

    //Reading name of the input file
    cout << " Enter name of input file: ";
    cin >> source;

    //Reading name of the first half destination file
    cout << " Enter name of destination first half file: ";
    cin >> dest1;

    //Reading name of the second half destination file
    cout << " Enter name of destination second half file: ";
    cin >> dest2;

    //Splitting data into two files
    split(source, dest1, dest2);

    //Reading name of the second half destination file
    cout << " Enter name of destination file for Merge: ";
    cin >> destination;

    //Merging files
    merge(dest1, dest2, destination);

    cout << " ";
    return 0;
}

//Splitting data to two files
void split(char sourceFile[], char destFile1[], char destFile2[])
{
    char ch;
    int i=0;

    //Opening source file in read mode
    fstream fin(sourceFile, ios::in);

    //Opening destination file in write mode
    fstream fout1(destFile1, ios::out);

    //Opening destination file in write mode
    fstream fout2(destFile2, ios::out);

    //Iterating over data in file 1
    while(fin.get(ch))
    {
        //Checking for even position
        if(i%2 == 0)
        {
            //Writing to first file
            fout1 << ch;
        }
        else
        {
            //Writing to second file
            fout2 << ch;
        }

        //Incrementing i
        i += 1;
    }

    cout << " Encryption process completed!!! ";

    //Closing files
    fin.close();
    fout1.clear();
    fout2.close();
}

//Merging files
void merge(char sourceFile1[], char sourceFile2[], char destFile[])
{
    char ch;
    int i=0;

    //Opening source file in read mode
    fstream fin1(sourceFile1, ios::in);
    fstream fin2(sourceFile2, ios::in);

    //Opening destination file in write mode
    fstream fout(destFile, ios::out);

    //Iterating over data in file 1
    while(true)
    {
        //Checking for even position
        if(i%2 == 0)
        {
            //Reading a character
            if(!fin1.get(ch))
            {
                break;
            }
        }
        else
        {
            //Reading a character
            if(!fin2.get(ch))
            {
                break;
            }
        }

        //Writing a character
        fout << ch;

        //Incrementing i
        i += 1;
    }

    cout << " Decryption process completed!!! ";

    //Closing files
    fin1.close();
    fin2.clear();
    fout.close();
}

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