Controllerei anche la fwrite. Non è che hai invertito i campi?
codice:
ret = fwrite(buff,dim,1 ,fp);