Fixed the logical errors with fread and fwrite