Sign in to follow this  
Hannibal111111

Reading tile info from file

Recommended Posts

I am developing a pacman clone and working on reading the tiles from a file. Its not working the way I hoped. For some reason when I check the first character it comes up as 40'('. It should read in (. Below is my how my text file looks: (hhhhhhhhhhhhhhhhh) |.................| |p().(h>.o.<h).()p| |.[].rh>.b.<hr.[].| |....|.......|....| |.<>.|.<hdh>.|.<>.| |....|...|...|....| [hh).rh>.b.<hl.(hh] eee|.|eeeeeee|.|eee eee|.be(>e<)eb.|eee eee|.ee|eee|ee.|eee eee|.oe[hhh]eo.|eee eee|.|eeeeeee|.|eee (hh].be<hdh>eb.[hh) |....eeee|eeee....| |.<dhh>eebee<hhd>.| |p.|...eeeee...|.p| |>.b.o.<hdh>.o.b.<| |....|...|...|....| |.<>.[h>.|.<h].<>.| |........|........| [hhhhhhhhhhhhhhhhh] Here is my code:

//open up tilemap file
FILE *f = fopen("Data/tilemap.txt","r");
if (f == NULL)
	exit(0);

while(!feof(f))
{
	for(i=0;i<19;i++)
	{
		for(j=0;j<22;j++)
		{
			//insert into tile map array
			cTemp = fgetc(f);
			if(cTemp == 'p' || cTemp == 'e' || cTemp == '.')
			{
				Level[i][j].blocked = false;
			}
			else
			{
				Level[i][j].blocked = true;
			}
			Level[i][j].type = cTemp;
		}
	}
}

//close file
fclose(f);


Share this post


Link to post
Share on other sites
40 is the decimal value of the '(' character code. Looks to me like everything is working correctly.

You can find the numeric values of other characters here. Note that those values are only valid for ASCII encoding and compatible Unicode formats (UTF-7, UTF-8); certain Unicode formats may have different values.

Share this post


Link to post
Share on other sites
Yep, that's correct - the debugger will show both the number value and the corresponding character.

For the second question, tweak it thusly:

		for(j=0;j<22;j++)
{
//insert into tile map array
do
{
cTemp = fgetc(f);
} while(cTemp < 35);

if(cTemp == 'p' || cTemp == 'e' || cTemp == '.')
{
Level[i][j].blocked = false;
}
else
{
Level[i][j].blocked = true;
}
Level[i][j].type = cTemp;
}



This will read one character, check if it is less than 35, and if so, read another character. It will continue trying to read until it finds what it wants.

Be warned though that it won't behave nicely if the file doesn't have enough data in it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this