Use write() for binary output: file.write(reinterpret_cast<const char*>(&data), sizeof(data));. This dumps raw bytes from memory to file. Fast and compact, but produces unreadable binary files.
You can serialize entire arrays: file.write(reinterpret_cast<char*>(arr), sizeof(arr));. The file stores bytes exactly as they appear in RAM. Reading back requires matching types and sizes.
Binary files aren't portable across systems with different endianness (byte ordering). An integer written on x86 may read wrong on ARM. For portability, use text formats or explicit byte ordering.