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

Please work on C++ You can ONLY use given #include code you are working on #incl

ID: 3889618 • Letter: P

Question

Please work on C++

You can ONLY use given #include

code you are working on

#include <fstream>
#include <string>
#include <algorithm>

void unzip_file(const std::string& filename) {
//TODO
}

Test code(cannot change):

#include <iostream>

int main() {
std::string testFilename;

// Tests your unzip_file function on test01 through test09
for (int i = 1; i <= 9; ++i) {
testFilename = std::string("test0") + (char) (i + 48);
// Test your unzip_file function.
unzip_file(testFilename);
if (cmp_files(testFilename + "-unzipped", testFilename + "-unzipped-expected")) {
std::cout << "Unzipping the file went as expected!" << std::endl;
} else {
std::cout << "Something went wrong unzipping the file. :(" << std::endl;
return -1;
}
}

2) 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 7 2 4 6 8 0102030409101112171819202526272805060708131415162122232429303132 To accomplish this . Open the file with the given filename for input in binary mode . Read the 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" . Note: you should not modify the original file when doing this. CSE 250 Fall 2017 Assumptions you can make for unzip file(...): The file referred to by filename exists. The file referred to by filename is not empty The number of bytes in the file is divisible by 4. You have read/write permission on the directory

Explanation / Answer

Given below is the implementation of unzip_file(), please use this code in your files and test it test code. Since the test files are not provided, I can not test it. Post a comment in case of any issues and I shall fix it. If works well for you, please don't forget to rate the answer . Thank you.

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]; //4 bytes
  
//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;
  
}