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

Recommended Answers

All 4 Replies

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;
}

To get 'non-destructive writes', open the file specifying also the ios::in mode.

Yes that did the trick thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.