If I want to use c++ to write a number (let’s say it is in hex format) to a binary file, for example,
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int num = 0xabcd;
ofstream outfile("tmp.bin", ios::out | ios::binary);
outfile.seekp(0x8000, ios_base::beg);
outfile.write(num, sizeof(num));
return 0;
}
This program would not run since ostream& write() only takes char* chars[] as its first argument whereas I need to write a int type number num to the binary file “tmp.bin”.
The desired result is that when I view the “tmp.bin” in hex mode, I can see ab cd at address 0x8000. I am wondering if this is possible, and could anyone help me please? Thank you very much.
You can cast your
intto achar*