handle fread/fwrite errors more gracefully