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

Data structure in C++ help needed! _____________________________________________

ID: 3888275 • Letter: D

Question

Data structure in C++ help needed!

____________________________________________________________

Here's the code you need to edit, you may only use these three headers!

PLEASE ONLY USE THESE THREE HEADERS! OTHERWISE, YOU WILL GET A BAD RATE

I do rate the answer, so please answer it as accurate as possible

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 10 4 1 5 ] 6 ] 7 j 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 1 6 8 1 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

Explanation / Answer

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

void unzip_file(const std::string& fileName)
{
   //new file name to store the output data
   std::string newFileName = fileName + "_unzipped";
   //opening the input file std::ifstream::ate is used to set
   //the file pointer at end of the file(used to calculate size fo file)
   //std::ifstream::binary flag is used to open file in binary mode
   std::ifstream file(fileName.c_str(), std::ifstream::ate |
               std::ifstream::binary);
   //creating output file
   std::ofstream outputFile(newFileName.c_str(), std::ofstream::binary);
   //get the file size
   unsigned long long int fileSize = file.tellg();
   //string to store the content of original file
   std::string fileData;

   //return if any file not opened successfully
   if(!file.is_open() && !outputFile.is_open()) return;

   //bring the file pointer to the beggining of file
   file.seekg(0, std::ios::beg);
   //store the data of file in the string
   fileData.assign(std::istreambuf_iterator<char>(file),
                std::istreambuf_iterator<char>());

   //store odd positional 4 byte blocks to the output file
   for (unsigned long long int i = 0; i < fileSize; )
   {
       outputFile << fileData[i];
       i++;
       //if 4 bytes of data written jump by 4 bytes
       if (i%4 == 0) i += 4;
   }

   //store even postioned 4 byte blocks after all odd blocks
   for (unsigned long long int i = 4; i < fileSize; )
   {
       outputFile << fileData[i];
       i++;
       if (i%4 == 0) i += 4;
   }
}

You can check it by

#include <iostream>

int main(int argc, char const *argv[])
{
   const char* fileName = "file";
   unzip_file(fileName);
   return 0;
}
suppose file contains data : 000011110000111100001111

the output data will be : 000000000000111111111111