Reading into an array

closed account (GbX36Up4)
So my program needs to read a 80 by 60 array of integers from a text file into an array. The process seems to work, but I'm not sure the file is being read correctly. My code so far:
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
int curmap[80][60]; // Stores sprite data for the map
// Some other crap...
// Load from file
void LoadMapFromFile()
{
	fstream myfile;
	myfile.open(mapfile);
	for (int a = 0; a < 80; ++a)
	{
		for (int b = 0; b < 60; ++b)   
		{
			myfile >> curmap[a][b];
		} 
	}
	myfile.close();
}

// Draws to screen
void LoadMap()
{
	for (int a = 0; a < 80;++a)
	{
		for (int b = 0; b < 60; ++b)   
		{
			if(curmap[a][b] == 0)
			{
				rectfill(map, a*32, b*32, a*32+32, b*32+32, makecol(0, 0, 0));
			}

			else if(curmap[a][b] == 1)
			{
				rectfill(map, a*32, b*32, a*32+32, b*32+32, makecol(255, 0, 0));
			}
		} 
	}
}

The format of the text file is as follows.
1 1 1 1 1 1 1 1 1 1 1 1 etc
Each line contains 80 characters each with a space between them (there is no space at the end of the line.) It is like that for 60 lines.

Here are some photos of the result.
http://www.thepicturebin.com/images/Logart/faillolol.jpg
http://www.thepicturebin.com/images/Logart/htreghbtrtr.jpg
It should be all black tiles with red ones around the border, but I don't know what the f*** that is.
Any and all help appreciated. Thanks. :)
map, a*32, b*32, a*32+32, b*32+32,

This looks wrong. Are you sure these parameters are correct for an 80 by 60 grid?
closed account (GbX36Up4)
Each square is 32 by 32. The first two are the x and y of the top left, the second two are the x and y of the bottom right, so I add an extra 32 to them. map is just the bitmap It's being drawn to.
closed account (GbX36Up4)
I did a bit of debugging:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

void LoadMap()
{
	for (int a = 0; a < 80;++a)
	{
		for (int b = 0; b < 60; ++b)   
		{
			if(curmap[a][b] == 0)
			{
				//rectfill(map, a*32, b*32, a*32+32, b*32+32, makecol(0, 0, 0));
			}

			else if(curmap[a][b] == 1)
			{
				//rectfill(map, a*32, b*32, a*32+32, b*32+32, makecol(255, 0, 0));
			}
			textprintf_ex(map, font, a*10, b*10, makecol(0, 0, 0), -1, "%d", curmap[a][b]);
		} 
	}
}


And found this:
http://www.thepicturebin.com/images/Logart/debug.jpg

Each number represents the value inside of the array, with the very top left being (0, 0). This means the problem is somewhere inside of LoadMapFromFile...
Last edited on
closed account (GbX36Up4)
After trying to fix it for a while I decided to try a new method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void LoadMapFromFile()
{
	FILE * myfile;
	
	myfile = fopen(mapfile, "r");
	for (int a = 0; a < 80; ++a)
	{
		for (int b = 0; b < 60; ++b)   
		{
			fscanf_s(myfile, "%d", &curmap[a][b]);
		} 
	}
	fclose(myfile);
}


And it gave me the same results...
Here's my text file's format:
http://www.thepicturebin.com/images/Logart/txtfile.jpg
closed account (GbX36Up4)
Well, I finally got it to work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void LoadMapFromFile()
{
	FILE * myfile;
	
	myfile = fopen(mapfile, "r");
	int b = 0;
	for (int a = 0; a < 4800; ++a)
	{
		if(a/80 > b)
			{
				++b;
			}
		fscanf(myfile, "%d", &curmap[a%80][b]); 
	}
	fclose(myfile);
}


Now I'm going to get some f'in sleep.
Topic archived. No new replies allowed.