fgetc returning -1?

Jun 28, 2012 at 9:25am
Hey all

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

1
2
3
4
5
6
7
8
9
10
EGGFCCCDEC
ABFGDDCGED
EDAFDEACGF
ADFECBCDEB
DBFADFAFGB
DBGGDEFAAB
AGEDABCBFB
DFBBCEGCCG
DCCGCBBCGD
GGEGGBBGDD


heres the function...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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)...

PATH: map1/0-0.txt
CHAR: ÿ -1 ---- 0 0
PATH: map1/0-1.txt
CHAR: ÿ -1 ---- 0 0
PATH: map1/0-2.txt
CHAR: ÿ -1 ---- 0 0
PATH: map1/0-3.txt
Last edited on Jun 28, 2012 at 9:29am
Jun 28, 2012 at 9:37am
What is errno, and what does strerror(errno) say?
Jun 28, 2012 at 9:54am
hmm, the program does not crash at this point, is errno the return number when the program crashes?

EDIT: Ok i tried at errno and strerror, this is the results

errno returns: 17
strerror(errno) returns: 11075112
Last edited on Jun 28, 2012 at 9:57am
Jun 28, 2012 at 10:34am
Can you show the definition of defineMap
Jun 28, 2012 at 10:39am
errno == 17 means: "file exists" which makes not too much sense in case of read. But it may not opened for other reasons. Check if mapFile != NULL

you should rather use ferror():

http://www.cplusplus.com/reference/clibrary/cstdio/ferror/
Last edited on Jun 28, 2012 at 10:40am
Jun 28, 2012 at 10:43am
You should check whether mapFile is equal to NULL after opening it

1
2
mapFile = fopen(filePath, "r");
if ( !mapFile ) { /* some action */ }
Jun 28, 2012 at 11:11am
Hey all thanks for your help, ill try the NULL thingo in a sec

this is for kbw:

1
2
3
4
5
6
7
8
9
10
11
12
13
//class for map stuff!
class defineMap
{
	public:
	void loadChunk(int x, int y, char* filePath);
	void unLoadChunk(int x, int y);
	void loadTextures(char* fileName);
	void drawMap(SDL_Surface* screen);
	//void defineMap::loadMap(SDL_Surface* screen);
	private:
		chunk map[maxLoad * maxLoad];
		vector<texture> textures;
};


just the code for the defineMap class
Jun 28, 2012 at 1:14pm
Thanks, and what is the definition of map in:
 
chunk map[maxLoad * maxLoad];
Jun 29, 2012 at 5:50am
Sorry for late reply

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...
Last edited on Jun 29, 2012 at 5:54am
Jun 29, 2012 at 5:59am
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!)
Last edited on Jun 29, 2012 at 5:59am
Topic archived. No new replies allowed.