use fwrite instead of fputs, and other changes