Write a C++ program to Implement the encryption and decryption functions for suc
ID: 673444 • Letter: W
Question
Write a C++ program to Implement the encryption and decryption functions for such a scheme that will split the original file into two new files and vice versa. One of these two new files will containt all the characters in the original file in odd position and the other will contain all characters in the even position. For example, if the original file contained the text: Hello World! then the 1st file would contain : HloWrd and the 2nd file would contain: el ol!
1st function:
void split(char sourceFile[ ], char destFile1[ ], char dest File2[ ]);
Function Should:
I. open sourceFile as an input file stream
II. Open destFile2 and destFile2 as output file streams
III. Read each character from sourceFile
IV. If it's an even numbered character (starting at zero), place it in destFile1
V. Else place it in destFile2
VI. Close the files.
2nd function:
void merge(char sourceFile1[ ], char sourceFile2[ ], char destFile[ ]);
Function Should:
reverse the previous operations
Testing:
I Ask the user for the name of the input file
II. Ask the user for teh names of the two half files
III. Call the split function with the filenames as parameters
IV. Ask the user for the name for the recombined file
V. Call the merge function with the appropriate filenames as parameters
Also:
Give detailed directions and warnings to user
Check to see if file opened correctly
Reset teh file pointers to re-check the file existance (if needed)
Close the output file
Redundancies for file permissions and pre-existent files to prevent overwrite
Explanation / Answer
//#include "stdafx.h"
#include <fstream> //srand
#include <ctime> //time
#include <cmath>
#include <iostream>
using namespace std;
void split(char sourceFile[], char destFile1[], char destFile2[]){
int line;
ifstream s(sourceFile);
ofstream f1(destFile1);
ofstream f2 (destFile2);
if (s.is_open()&&f1.is_open()&&f2.is_open())
{
int i=0;
char t[0];
while ( s.peek()!=EOF )
{
s.read(t,1);
if(i%2==0){
f2<<t[0];
}else{
f1<<t[0];
}
}
s.close();
f1.close();
f2.close();
}
else {cout << "Unable to open file";
}
}
void merge(char sourceFile[], char destFile1[], char destFile2[]){
int line;
ofstream s(sourceFile);
ifstream f1(destFile1);
ifstream f2 (destFile2);
if (s.is_open()&&f1.is_open()&&f2.is_open())
{
int i=0;
char t[0];
while ( f1.peek()!=EOF&&f1.peek()!=EOF )
{
if(i%2==0){
f2.read(t,1);
s<<t[0];
}else{
f1.read(t,1);
s<<t[0];
}
}
s.close();
f1.close();
f2.close();
}
else {cout << "Unable to open file";
}
}
int main(){
char s[200],f1[200],f2[200];
cout<<"Enter the source file name: ";
cin>>s;
cout<<"Enter the destination file name1: ";
cin>>f1;
cout<<"Enter the destination file name2: ";
cin>>f2;
split(s,f1,f2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.