I have the following case study:
The variable "x_y" holds the content of a file in binary mode, read as follows:
std::string x_y;
std::ifstream fnb(fn, std::ios::in | std::ios::binary);
std::stringstream file_jh;
file_jh << fnb.rdbuf();
x_y = file_jh.str();
What I'm trying to do is to read/copy an std::uint32_t value from x_y, do some computation on it, and saving it back at the same address as follows:
std::uint32_t delta = 0x00012345;
std::uint32_t temp_val = 0;
for (size_t i = 0; i < x_y.size(); i += 4)
{
x_y.copy((char*)&temp_val, sizeof(std::uint32_t), i);
temp_val -= delta;
delta += 20;
x_y.replace(i, sizeof(std::uint32_t), (char*)&temp_val);
}
The problem I'm having is that it works only for the first dword value, but for the rest of the data its producing incorrect results. Additionally, it seems to be messing up the offsets too, which I don't understand why!
Could you please share any suggestions you might have, either by improving on above code or something different.
Thank you.