3 ms·
I understand the reasoning. If you have a string each letter is an individual character. You wouldn't pack two ASCII letters into a a char just because your cha
by pantalaimon 4y ago
I understand the reasoning.
If you have a string each letter is an individual character.
You wouldn't pack two ASCII letters into a a char just because your char is 16 bit.
On such a platform, if you have the string "Hello" and would write the chars un-truncated you'd end up with "H\0e\0l\0l\0o\0" in the file.
However, if you are not writing a string, but binary data, this would mean you now loose every 2nd 8-bit byte.
- theamk 4y agoIf you are writing binary data from memory, _and_ want to make sure any system can read it no matter what endianness it uses, you might need something like this: buf[0] = (data >> 24) & 0xFF; buf[1] = (data >> 16) & 0xFF; buf[2] = (data >> 8) & 0xFF; buf[3] = data & 0xFF; and if you want this code to work on all systems (including wide-char ones), you need to make sure fwrite truncates too.