Data structure in C++ help needed. How do we restore a file back to it original?
ID: 3888669 • 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>
using namespace std;
void zip_file(const std::string& filename) {
ifstream in;
int i,j=0;
string data, rearrange="00000000";
in.open(filename.c_str());
getline(in, data);
for(i=0;data[i]!='';i = i+2){
rearrange[j] = data[i];
j++;
}
for(i=1;data[i]!='';i = i+2){
rearrange[j] = data[i];
j++;
}
ofstream f;
f.open("test-unzipped");
f << rearrange<<" ";
}
void restore(const std::string& filename){
string res;
ifstream in;
in.open("test");
getline(in, res);
ofstream f;
f.open(filename.c_str());
f << "Data restored : -"<< res;
}
int main(){
zip_file("test");
//restore("test-unzipped");
}
// Just Remove the comment from function restore and you can restore.
//since you are not using #include<iostream> we can not make user input to show the rearranged file and restored file separately.
//So first run with comment in restore() and check it will create unzipped-test is created with rearranged value.
//Then remove comment from restore() and run the code and check unzipped-test is modified.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.