0
fstream input("test1.docx", ios::in | ios::binary); 
uint8_t * buffer = new uint8_t;
vector<uint8_t> arr; //vector to save value of file
fstream output("test2.docx", ios::out | ios::binary);
while (input.good())
{
    input.read((char*)(buffer), 1);
    arr.push_back((*buffer));
}

After doing this, i have a vector of values. Can i build a new file "test2.docx" from this vector? This new file will be exactly the same as the old file "test1.docx"

Mehdi Mostafavi
  • 738
  • 6
  • 21
  • "Can i build a new file "test2.docx" from this vector?" - yes – ForceBru Apr 11 '20 at 13:26
  • 1
    You don't need to create `buffer` as a pointer, you could use the address-of operator with a non-pointer variable. As in `uint8_t buffer; input.read(reinterpret_cast(&buffer), 1);` – Some programmer dude Apr 11 '20 at 13:29
  • 1
    Also please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) Your loop `while (input.good())` is just as bad. – Some programmer dude Apr 11 '20 at 13:30
  • At the end of the file, `input.read` will fail, yet the code doesn't check for the failure, and does an `arr.push_back` of the garbage value. – Eljay Apr 11 '20 at 13:30

0 Answers0