Data structure in C++ help needed. How do we store a file back to it original? f
ID: 3888472 • Letter: D
Question
Data structure in C++ help needed.
How do we store a file back to it original?
for an example.
The original file was 11115555333344442222, then we rearrange the file to 11112222333344445555.
Here's my question how do you restore 11112222333344445555 back to 11115555333344442222?
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.
Explanation / Answer
//Please see the below code
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
char * file_name="input.txt";
fstream file(file_name);
string orgStr;
while(getline(file, orgStr))
{
//after modification the string becomes 11112222333344445555
orgStr="11112222333344445555" ;
}
file.seekg(0,ios::beg);//Move the read pointer to begning
file.seekp(0,ios::beg); //Move the write pointer to begning
file << orgStr;
file.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.