You are ignoring the nBytes
return value from read()
, and instead always writing sizeof buf
bytes to the output. That's wrong, for partial reads (where nBytes
is less than sizeof buf
) you are injecting junk into the written stream.
The write should of course use nBytes
, too.
Also: the write can fail, and write less than you requested, so you need to loop it until you know that all bytes have been written, or you get an error from it.