Clarification is needed with reinterpret_cast
.
I am building .wav
file where various data is inputted as 2 or 4 bytes in hexadecimal bits (if I am correct).
Then, I came across a simple function like this:
void writeToFile(std::ofstream &file, int value, int size)
{
file.write(reinterpret_cast<char*>(&value), size);
}
As far as I understand what reinterpret_cast
does is that it changes the data type in the address a pointer points to!
For example:
int* intPtr{ new int{7} };
char* charPtr{ reinterpret_cast<char*>(intPtr) };
So here we got a new pointer that is supposed to point to char
value instead of original int
.
If so, then I don't understand validity of the above function void writeToFile(std::ofstream &file, int value, int size)
, because if I follow my logic, then in this function an int value
can't be written to file
as the pointer supposedly points to char
value, not int
!
P.S. Even if a file is opened in std::ios::binary
mode, how does it exactly work? How do bits of int
become bits of char
?