If you are not concerned about endian issues, you can take a shortcut:
1 2 3 4 5 6 7 8 9
// note: #include <cstdint> for int32_t and related types
int32_t v;
myfile.read( reinterpret_cast<char*>(&v), 4 );
cout << v; // prints 511 on little endian systems
// (or -16711680 on big endian systems)
myfile.write( reinterpret_cast<char*>(&v), 4 );
So as you can see: easy but non portable.
If you want the portable version:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// to read:
uint8_t b[4]; // use an unsigned type like uint8_t... don't use char
myfile.read( reinterpret_cast<char*>(b), 4 );
uint32_t v = b[0] |
(b[1] << 8) |
(b[2] << 16) |
(b[3] << 24);
cout << v; // now prints 511 regardless of system endianness
// to write:
b[0] = (v & 0xFF);
b[1] = ((v >> 8) & 0xFF);
b[2] = ((v >> 16) & 0xFF);
b[3] = ((v >> 24) & 0xFF);
myfile.write( reinterpret_cast<char*>(b), 4 );