Sign in to follow this  

fread/fwrite refuses to cooperate.

This topic is 4718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Everything seems to save fine with fwrite, as the return value is 1 indicating the full object was saved. The problem comes when reading the file. For some reason fread returns 0. I can't, for the life of me, figure it out. To make matters worse, this doesn't happen all the time. I believe the problem is with fwrite, and it's not saving everything as it's supposed to. Otherwise fread SHOULDN'T have any problems. Argh. I should note where it returns 0: fread returns 0 when it reads in the tiles: // Read all tiles. map_vector_saver_t* m_pSavedVect = new map_vector_saver_t(); fread(m_pSavedVect, sizeof(map_vector_saver_t), 1, pFile); fread returns 0. Here's the code:
typedef struct tagENTITYSAVER
{

	POINT	Point;
	DWORD	CloneID;

} entity_saver_t;

typedef struct tagTILESAVER
{

	// # = layer.
	DWORD	TileID1;
	DWORD	TileID2;
	DWORD	TileID3;
	BOOL	Collision;

} tile_saver_t;

typedef struct tagMAPVECTORSAVER
{

	tile_saver_t	Tiles[MAP_ROWS][MAP_COLS];

} map_vector_saver_t;

BOOL Map::Load(DWORD level)
{
	m_level = level;

	// Open file.
	TCHAR fileName[LVL_BUFF_SIZE] = { 0 };
	this->CreateFileName(level, fileName);
	FILE* pFile = _tfopen(fileName, _T("r"));

	if (pFile == NULL) {
		return FALSE;	// Couldn't load level.
	}

	this->Clear();	// Map data needs to be cleared.

	// Load extra saved space.
	DWORD temp = 0;

	// NOTE:
	// Extra space, "just in case."
	fread(&temp, sizeof(DWORD), 1, pFile);
	fread(&temp, sizeof(DWORD), 1, pFile);
	fread(&temp, sizeof(DWORD), 1, pFile);
	fread(&temp, sizeof(DWORD), 1, pFile);

	// Load tileset.
	DWORD graphicID = 0;
	fread(&graphicID, sizeof(DWORD), 1, pFile);
	this->InsertTileset(graphicID);

	// Read all tiles.
	map_vector_saver_t* m_pSavedVect = new map_vector_saver_t(); 
	fread(m_pSavedVect, sizeof(map_vector_saver_t), 1, pFile);

	// Build individual tiles.
	DWORD x = 0, y = 0;
	for (DWORD row = 0; row < MAP_ROWS; ++row) {

		for (DWORD col = 0; col < MAP_COLS; ++col) {

			m_pVect->Tiles[row][col].TileID1		= m_pSavedVect->Tiles[row][col].TileID1;
			m_pVect->Tiles[row][col].TileID2		= m_pSavedVect->Tiles[row][col].TileID2;
			m_pVect->Tiles[row][col].TileID3		= m_pSavedVect->Tiles[row][col].TileID3;
			m_pVect->Tiles[row][col].Collision		= m_pSavedVect->Tiles[row][col].Collision;
			m_pVect->Tiles[row][col].Rect.SetRect(x, y, x + TILE_WIDTH, y + TILE_HEIGHT);

			x += TILE_WIDTH;

		}

		y += TILE_HEIGHT;
		x = 0;
	}

	delete m_pSavedVect;

	// Load player.
	entity_saver_t savedPlayer;
	fread(&savedPlayer, sizeof(entity_saver_t), 1, pFile);

	// Build valid player. Pos 0/0 = none.
	if (savedPlayer.Point.x != 0 && savedPlayer.Point.y != 0) {
		entity_t* pPlayer = new entity_t(savedPlayer.CloneID);
		pPlayer->X = (FLOAT) savedPlayer.Point.x;
		pPlayer->Y = (FLOAT) savedPlayer.Point.y;
		this->InsertPlayer(pPlayer);
	}

	// Close file.
	fclose(pFile);

	return TRUE;
}

BOOL Map::Save() const
{
	// Open file.
	TCHAR fileName[LVL_BUFF_SIZE] = { 0 };
	this->CreateFileName(m_level, fileName);
	FILE* pFile = _tfopen(fileName, _T("w"));

	if (pFile == NULL) {
		return FALSE;	// Couldn't save level.
	}

	// Save extra space.
	DWORD temp = 0;

	// NOTE:
	// Extra space, "just in case."
	fwrite(&temp, sizeof(DWORD), 1, pFile);
	fwrite(&temp, sizeof(DWORD), 1, pFile);
	fwrite(&temp, sizeof(DWORD), 1, pFile);
	fwrite(&temp, sizeof(DWORD), 1, pFile);

	// Save tileset.
	DWORD graphicID = m_pTileset->GetResourceID();
	fwrite(&graphicID, sizeof(DWORD), 1, pFile);

	// Create storage for tiles.
	map_vector_saver_t* m_pStorageVect = new map_vector_saver_t(); 

	// Build invidiaul tiles for storage.
	for (DWORD row = 0; row < MAP_ROWS; ++row) {

		for (DWORD col = 0; col < MAP_COLS; ++col) {

			m_pStorageVect->Tiles[row][col].TileID1		= m_pVect->Tiles[row][col].TileID1;
			m_pStorageVect->Tiles[row][col].TileID2		= m_pVect->Tiles[row][col].TileID2;
			m_pStorageVect->Tiles[row][col].TileID3		= m_pVect->Tiles[row][col].TileID3;
			m_pStorageVect->Tiles[row][col].Collision	= m_pVect->Tiles[row][col].Collision;

		}

	}

	// Store tiles all in one go.
	fwrite(m_pStorageVect, sizeof(map_vector_saver_t), 1, pFile);
	delete m_pStorageVect;

	// Build player storage.
	entity_saver_t playerStorage;
	::ZeroMemory(&playerStorage, sizeof(entity_saver_t));

	if (m_pPlayer != NULL) {
		playerStorage.CloneID = m_pPlayer->CloneID;
		playerStorage.Point.x = (LONG) m_pPlayer->X;
		playerStorage.Point.y = (LONG) m_pPlayer->Y;
	}

	// Store player.
	fwrite(&playerStorage, sizeof(entity_saver_t), 1, pFile);

	// Close file.
	fclose(pFile);

	return TRUE;
}


Share this post


Link to post
Share on other sites
Try changing the access modes to binary ("rb", "wb"). It tends to fix many mysterious IO errors.

Share this post


Link to post
Share on other sites
Sign in to follow this