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

Consider the following scheme for encrypting a text file. The original file is s

ID: 3757008 • Letter: C

Question

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:

Open sourceFile as an input file stream,

Open destFile1 and destFile2 as output file streams

Read each character from sourceFile and

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

Else place it in destFile2

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:

Ask the user for the name of the input file.

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

Call the split function with the filenames as parameters.

Ask the user for the name of the recombined file.

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

ANSWER :

#include <bits/stdc++.h>
using namespace std;
void split(char sourceFile[],char destFile1[],char destFile2[])
{
ifstream infile(sourceFile);   //open the source file
ofstream outfile;
ofstream out;
char c;
int count=0;
outfile.open(destFile1); //open the splitted even char file
out.open(destFile2);    //open the splitted odd char file
while(infile.get(c))    //reading char by char the source file
    {
       if(count==0)
   {
      outfile<<c;   //Writing even character into file
   }
      else if(count==1)
   {
      out<<c;     //writing odd characters into file
   }
      count++;
      count%=2;
    }
}
void merge(char sourceFile1[],char sourceFile2[],char destFile[])
{
ifstream infile,in;
ofstream outfile;
char c;
//opening two splitted file
infile.open(sourceFile1);
in.open(sourceFile2);
//opening the file where combined result is written
outfile.open(destFile);
int count=0;
//Taking one by one character each from odd and even file to merge
while(!infile.eof() || !in.eof())
    {
      if(count==0)
   {
      infile.get(c);
      outfile<<c;
   }
      else if(count==1)
   {
      in.get(c);
      outfile<<c;
   }
      count++;
      count%=2;
    }
}
int main()
{
char sourcefile[256],destfile1[256],destfile2[256],comFile[256];
cout<<"Enter the source file ";
cin>>sourcefile;
cout<<"Enter the name of splitted files ";
cin>>destfile1>>destfile2;
split(sourcefile,destfile1,destfile2);
cout<<"Enter the name of combine file ";
cin>>comFile;
merge(destfile1,destfile2,comFile);

}

#include <bits/stdc++.h>
using namespace std;
void split(char sourceFile[],char destFile1[],char destFile2[])
{
ifstream infile(sourceFile);   //open the source file
ofstream outfile;
ofstream out;
char c;
int count=0;
outfile.open(destFile1); //open the splitted even char file
out.open(destFile2);    //open the splitted odd char file
while(infile.get(c))    //reading char by char the source file
    {
       if(count==0)
   {
      outfile<<c;   //Writing even character into file
   }
      else if(count==1)
   {
      out<<c;     //writing odd characters into file
   }
      count++;
      count%=2;
    }
}
void merge(char sourceFile1[],char sourceFile2[],char destFile[])
{
ifstream infile,in;
ofstream outfile;
char c;
//opening two splitted file
infile.open(sourceFile1);
in.open(sourceFile2);
//opening the file where combined result is written
outfile.open(destFile);
int count=0;
//Taking one by one character each from odd and even file to merge
while(!infile.eof() || !in.eof())
    {
      if(count==0)
   {
      infile.get(c);
      outfile<<c;
   }
      else if(count==1)
   {
      in.get(c);
      outfile<<c;
   }
      count++;
      count%=2;
    }
}
int main()
{
char sourcefile[256],destfile1[256],destfile2[256],comFile[256];
cout<<"Enter the source file ";
cin>>sourcefile;
cout<<"Enter the name of splitted files ";
cin>>destfile1>>destfile2;
split(sourcefile,destfile1,destfile2);
cout<<"Enter the name of combine file ";
cin>>comFile;
merge(destfile1,destfile2,comFile);

}