Well, okteta reads binary data and prints it out in hex format (to be more readable than if you opened a binary file in a text editor). So what you need to write is binary data:
ofstream myfile;
myfile.open ("output.midi",ios::binary);
char buffer[44] = {0x4D,0x54,0x68,0x64,0x00,0x00,0x00,0x06,0x00,0x01,0x00,0x01,0x00,0x80,0x4D,0x54,0x72,0x6B,0x00,0x00,0x00,0x16,0x80,0x00,0x90,0x3C,0x60,0x81,0x00,0x3E,0x60,0x81,0x00,0x40,0x60,0x81,0x00,0xB0,0x7B,0x00,0x00,0xFF,0x2F,0x00};
myfile.write(buffer,44);
myfile.close();
Won't that just output the equivalent values as decimal integers instead of hex integers? Why not just use the hex stream manipulator:
#include <iomanip> //required for access to stream manipulators
const int BUFFER_SIZE = 44;
ofstream myFile;
myFile.open("output.mid", ios::out | ios::binary);
char buffer[BUFFER_SIZE] =
{0x4D,0x54,0x68,0x64,0x00,0x00,0x00,0x06,0x00,0x01,0x00,0x01,0x00,0x80,0x4D,0x54,0x72,0x6B,0x00,0x00,0x00,0x16,0x80,0x00,0x90,0x3C,0x60,0x81,0x00,0x3E,0x60,0x81,0x00,0x40,0x60,0x81,0x00,0xB0,0x7B,0x00,0x00,0xFF,0x2F,0x00};
for (int i = 0; i < BUFFER_SIZE; ++i) {
myFile << hex << buffer[i];
//NOTE: you may need the showbase manipulator and whitespace as well, I don't know exactly how MIDI files work
}
myFile << dec;
myFile.close();