I created a function which loads a very basic map file of the form:
1:1 2:1 1:1 2:2 2:2 2:2 ...
................... 2:1 1:1
However, when using fscanf to read in the file, I am getting some very odd behaviour.
Looking at the FILE variable I have set up to read the map, the base element of the ‘stream’ of the FILE seems to have read the file perfectly. However, the _ptr of the ‘stream’ of the FILE is missing the first number, and the last. So it gets read as:
:1 2:1 1:1 2:2 2:2 2:2 ...
................... 2:1 1:
and is generating an error.
Here is my function:
/**
* loads a map
*/
bool Map::LoadMap(char* tFile)
{
FILE* FileHandle = fopen(tFile, "r"); // opens map file for reading
if (FileHandle == NULL) // returns if the map file does not exist
return false;
for(int Y = 0; Y < MAP_HEIGHT; Y++) // iterates through each row
{
for(int X = 0; X < MAP_WIDTH; X++) // iterates through each column
{
Node tNode; // temp node to put in the map matrix
int tTypeID = 0;
int tNodeCost = 0;
fscanf(FileHandle, "%d:%d", tTypeID, tNodeCost);
tNode.SetPosition(X, Y);
tNode.SetType(tTypeID);
tNode.SetNodeCost(tNodeCost);
mMap[X][Y] = tNode; // inserts temp node into list
}
fscanf(FileHandle, "\n");
}
fclose(FileHandle);
return true;
}
Why this is happening?
You need to pass the addresses of the variables to
fscanf():Recommend checking the return value of
fscanf()to ensure success: