use fread/fwrite instead of fgets/fputs. fgets family are unsafe against
binary files that may contain NULL characters