Remarkable, sobering discussion of how hard it is to write bytes to a file robustly: https://danluu.com/deconstruct-files/
APIs are subtle, filesystems have different safety modes, they sometimes discard errors, not everyone complies with POSIX, and sometimes hardware doesn't meet its spec!