Hi everyone,
i wrote this small app and it works fine but it erases everything up to the offset and everything after it.
Anyone knows how to avoid that?

#include <iostream>
#include <fstream>

int main()
{
	int hex = 0x75;
	std::ofstream f("sample.exe", std::ios::binary);
	f.seekp(1169, std::ios::beg);
	f.write(reinterpret_cast<const char *>(&hex), 2);
	f.close();
	return 0;
}

regards

also use mode std::ios::ate (AT End)

The default mode for ofstream opening is std::ios::trunc, (TRUNCate), which deletes any previous content of the file. ios::ate preserves the file, allows you to move the file pointer anywhere within the current content.

It still doesn't work using this code.
It produces the same error again.

#include <iostream>
#include <fstream>
 
int main()
{
      int hex = 0x75;
      std::ofstream f("sample.exe", std::ios::binary | std::ios::ate);
      f.seekp(1169, std::ios::beg);
      f.write(reinterpret_cast<const char *>(&hex), 2);
      f.close();
      return 0;
}
This question has already been answered. Start a new discussion instead.