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!
It's always depressing looking at the SSH logs for my tiny server. I have it appropriately locked down, but it's a sobering reminder of how hostile the internet can be.