FWIW, this is the results of my tinkering. I think we're at wits end and you need to see, realistically, what you are not doing.
Code:
#include <fstream> // for file api
#include <vector> // is string type
#include <iostream> // for cout, cin, endl
#include <iomanip> // for hex manipulator
#include <cstdlib> // for rand ()
#define BYTES 16
int main ()
{
using namespace std;
fstream dat;
// generating data for proof-of-concept
dat.open ("dump.dat", ios::out | ios::binary);
if (dat.is_open())
{
char byte;
for (int i = 0; i < BYTES; i++)
{
byte = (rand () % 99) & 0xff;
cout << "writing value 0x" << hex << int(byte) << endl;
dat.write (&byte, sizeof byte);
if (!dat)
{
cout << "there was a problem writing the file" << endl;
return 0;
}
}
dat.close ();
}
else
{
cout << "buy a bigger hard drive" << endl;
return 0;
}
// prove we can read what we wrote
vector< char > string (BYTES);
dat.open ("dump.dat", ios::in | ios::binary);
if (dat.is_open())
{
dat.read (&string[0], BYTES);
if (dat)
{
for (int i = 0; i < string.size (); i++) {
cout << "read value 0x" << hex << int(string[i]) << endl;
}
dat.close ();
}
else {
cout << "problem reading the file" << endl;
return 0;
}
}
// now we edit it
vector< char >::size_type offset = 0;
cout << "enter the offset:" << endl;
cin >> offset;
// below statement refers to the exact byte,
// while protecting the bounds of string.
char &byte = string.at (offset);
cout << "enter the byte's new value:" << endl;
cin >> hex >> byte;
// finalize the edit
dat.open ("dump.dat", ios::out | ios::binary);
if (dat.is_open())
{
dat.write (&string[0], string.size ());
if (dat)
{
for (int i = 0; i < string.size (); i++) {
cout << "wrote value 0x" << hex << int(string[i]) << endl;
}
string.clear ();
dat.close ();
}
else {
cout << "problem rewriting the file" << endl;
return 0;
}
}
system ("pause");
// inspect your file -- it works!
return 0;
}
The example has challenges. Chiefly, because I don't know how to tell my present compiler to treat char as unsigned char, I definitely couldn't tell you how to do that with your software even if we both had the same compiler. So the output may look different, depending on the inputs, because of char being signed. If the appropriate switch were turned on, then the fact that this works would be much more apparent. I apologize for my ignorance. I also don't buy that you are just editing random bytes, and that there is a decent chance your file is record based, but I went and showed that you can indeed edit random bytes. If you were doing this with a record based file, then you would read the raw input into an instance of the very class that you saved instead of a string. The bit-fiddling part would have to be done on the class members and then re-saved.
Unless the file is really big, don't bother seeking it.