I am writting function for create and save WAV file but I don’t know how to send numbers to stream:
ofstream file;
file.open("sound.wav");
file << "RIFF";
file << (int) 32;
file << "WAVE";
I am trying to implement this WAVE file structure: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
and problem is here the output is like this:
RIFF32WAVE
The streaming operator
<<does formatted output – it converts values to text. This is not what you want for writing to a binary file format.Instead, you want to use the unformatted output functions:
putfor single bytes, andwritefor multiple bytes:UPDATE: as noted in the comments, you should also open the file in binary mode and inbue it with the classic “C” locale, to prevent anything from messing around with the bytes you write: