My first question is, why is it customary to use unsigned chars for writing to files in binary mode? In all of the examples I have seen, any other numerical value is casted to unsigned char before writing to the binary file.
My second question is, what's so bad about using stream operators to write to binary files? I've heard that read() and write() operators are best used for writing to binary files, but I don't really understand why that's the case. Using stream operators to write to binary files works fine for me IF I first cast the value to unsigned char.
float num = 500.5;
ostream file("file.txt", ios::binary);
file << num // results in gibberish when I try to read the file later
file << (unsigned char)num // no problems reading the file with stream operators
Thanks in advance.