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

Required function: void unzip_file(const std::string& filename) You get to work

ID: 3884634 • Letter: R

Question

Required function: void unzip_file(const std::string& filename) You get to work on your program to obfuscate a file and write a function named "unzip file." Your idea is to treat a file as blocks of 4 bytes of data and reorder these blocks so that to the world, this looks like nothing more than random data. To do this, you should open the file in binary mode and move every even positioned "block" to the end of the file, maintaining their order and leaving no space between each block. At the end, all odd positioned blocks are in increasing order, and all even positioned blocks should be in increasing order, where all even positions follow all odd positions. This output should be stored in a new file with the same name as the original file, but with the added suffix "-unzipped EXAMPLE: Suppose you have a 32-byte file named "test"' as follows (in hex); 0102030405060708091011121314151617181920212223242526272829303132 If we treat everything in the file groups of 4 bytes, we can view the file as the following 8 blocks: [ 1 2 3 4 5 6 7 8 0102030405060708091011121314151617181920212223242526272829303132 After running your function unzip file on this file "test", the blocks should be reordered as follows and the resulting data should be written to a file named "test-unzipped": [ 1 3 5 1 7 1 2 ] 4 ] 6 1 8] 0102030409101112171819202526272805060708131415162122232429303132 To accomplish this: Open the file with the given filename for input in binary mode. he entire file contents (you may want to compute the file size as well). Manipulate the data. write the output data to a file with the same name as the input file, but with the added suffix "-unzipped"

Explanation / Answer

Given below is the program to perform as specified. Sample input and output files are shown. Please don't forget to rate the answer if it helped. Thank you very much.

The output shown is for an input file names "letters". The program generates an output file named "letters-unzipped"

#include <iostream>
#include <fstream>
using namespace std;
void unzip_file(const string& filename);
int main()
{
string filename ;
cout << "Enter filename: ";
cin >> filename;
  
unzip_file(filename);
}

void unzip_file(const string& filename)
{
ifstream infile(filename.c_str(), ios::binary);
if(!infile.is_open())
{
cout << "Error opening input file " << filename << endl;
return;
}
  
string outfname = filename + "-unzipped"; //generate the output filename
ofstream outfile(outfname.c_str(), ios::binary);
  
if(!outfile.is_open())
{
cout << "Error opening output file " << outfname << endl;
return;
}
  
char block[4]; //4bytes
  
//first, write every odd block of 4 bytes to output file
while(true)
{
infile.read(block, 4); //read 4 bytes of data
if(infile.fail()) //if the read failed because of eof, then stop
break;
infile.seekg(4, ios::cur); //skip 4 bytes from current, i.e skip an even block
outfile.write(block, 4); //write 4 bytes of data to output file
}
  
//come back to begining of file, and start writing even blocks of 4 bytes
  
infile.clear();//clear any flags when eof was reached
infile.seekg(0, ios::beg); //go back to begginng of file
while(true)
{
infile.seekg(4, ios::cur); //skip 4 bytes from current location, i.e. skip an odd block
infile.read(block, 4); //read 4 bytes of data
if(infile.fail()) //read failed because of eof, then stop
break;
outfile.write(block, 4); //write 4 bytes of data to output file
}
  
infile.close();
outfile.close();
cout << "output file " << outfname << " generated." << endl;
  
}

input file: letters

aabbccddeeffgghhiijjkkll

output file: lettters-unzipped

aabbeeffiijjccddgghhkkll

program run:

Enter filename: letters
output file letters-unzipped generated.

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