Data structure in C++ help needed. How do we restore a file back to it original?
ID: 3888667 • Letter: D
Question
Data structure in C++ help needed.
How do we restore a file back to it original?
for an example.
Here's the code you need to edit:
_______________________________________________________________________________
In this case, you may ONLY use three headers which are #include<fstream> #include<algorithm> #include<string>
I do rate the answer, so please answer it as accurate as possible, and please don't use other header.
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 I 7 8 1 After running your function unzip_file on this file "test", the blocks should be reordered as follow:s 1 3 5 1 7 2 1 4 6 1 8] 0102030405060708091011121314151617181920212223242526272829303132 and the resulting data should be written to a file named "test-unzipped": 0102030409101112171819202526272805060708131415162122232429303132Explanation / Answer
#include <fstream>
#include <string>
#include <algorithm>
void zip_file(const std::string& filename) {
std::ifstream ifs(filename);
std::string line;
if(std::getline(ifs, line)){
ifs.close();
//return if file does not contain exactly 64 characters
if(line.length()!=64){
return;
}
std::string result;
for(int i = 0; i < 4; ++i){
std::string frnt;
std::string bak;
//get string at odd and even positions
for(int j = 0; j < 8; ++j){
frnt += line[i*8+j];
bak += line[i*8+j+32];
}
result += frnt + bak;
//write back to file
std::ofstream ofs(filename, std::ios::trunc);
ofs << result;
}
return;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.