I have been working on a tile based game (birds eye view type of thing) and im reading a map from a text file (or multiple).
Everything seems fine until I use fgetc on the char variable 'tempChar' to grab a char from the file, however, i printf to stdout and tempChar, for every fgetc i do, returns -1. Ive done tests and its not an error as the program would simply crash, it is reading the file incorrectly and i don't know why...
This is what the text file (one of the text files im trying to read from) looks like
void defineMap::loadChunk(int x, int y, char* filePath)
{
//this will determine which chunk to load into!
int whichChunk;
//Check the various chunks in the map to see whether they are in use
for (int i = 0; i < (maxLoad * maxLoad); i++)
{
if (defineMap::map[i].inuse != 1)
{
whichChunk = i;
break;
}
}
//Set the chunks x and y so we know where to put it!
defineMap::map[whichChunk].x = x;
defineMap::map[whichChunk].y = y;
//for the while loop!!
int i = 0;
int j = 0;
char tempChar;
//Create file stream and read the file
FILE* mapFile;
mapFile = fopen(filePath, "r");
//Actually read the file
while (!feof(mapFile))
{
tempChar = fgetc(mapFile);
printf("CHAR: %c %i ---- %i %i\n", tempChar, tempChar, i, j);
if (tempChar == '\n')
{
j++;
i = 0;
}
else
{
defineMap::map[whichChunk].map[i][j] = tempChar;
i++;
}
}
}
NOTE: this function is in the class 'defineMap', i don't think that would have anything to do with it...would it?
Here is some of stdout (from printf around line 34)...
kbw:
maxLoad is just a global variable defined that currently = 10.
to many other people:
I tried getting a return on 'mapFile' or the stream and its not NULL
I dont understand why its returning -1, Its not an error or anything because if it was an invalid file path, it would crash (at that particular point).
EDIT: I also tried ferror() and it returns 0...funny that...
I think i may have found the problem, in one of my previous functions, i create the files, but i think i forgot to close the stream, so all the files were still open when i tried to access them with a new stream (sorry for double post!)