don't understand basic file I/O

Started by
18 comments, last by Zahlman 15 years, 11 months ago
Hi, I'm really new to file I/O and don't know why the following code doesn't work:
// load maps
void LoadMaps()
{
	// this will store the file name
	char map_id[9];
	
	// this guy is an array of files, each one is a map
	FILE* maps[MAXNUMLEVELS];

	// run through a for loop and open all map files
	for (int h = 0; h < MAXNUMLEVELS; h++)
	{
		sprintf(map_id, "map0%d.txt", h);
		maps[h] = fopen(map_id, "r");
	}	
	
	// make sure map file isn't NULL
	if (maps[0] == NULL)
	{
		char* error_message = "Error: Unable to open map files.";
		WriteToLogFile(error_message);
	}
	
	// TODO: Load map data
	for (int p = 0; p < MAXNUMLEVELS; p++)
	{
		for (int m = 0; m < WORLDSIZE_X; m++)
		{
			for (int n = 0; n < WORLDSIZE_Y; n++)
			{
				g_levels.map[n][m] = fgetc(maps);
				
			}
		}
	}

	// maps have been loaded so close the files now
	for (int k = 0; k < MAXNUMLEVELS; k++)
	{
		fclose(maps[k]);
	}
}	

Here is the map file:
111111111111111111
111111111111111111
111111111111111111
111111111111111111
111111111111111111
111111111111111111
111111111111111111
111111111111111111
111111111111111111
111111111111111111
111111111111111111
111111111111111111
Advertisement
First, what do you mean by "doesn't work"? What are you expecting and what are you experiencing?

Second, when using C-style strings (ie, null-terminated character arrays), you need to allocate one extra char for the null. map05.txt is 9 characters; you need 10 to represent it as a C-string. If your MAXNUMLEVELS is 2 or 3 digits, then you need even more space... Failing to format your string properly will break many other routines, so that may be the first of your problems.

Third, why do you open all the files in one loop, read from them in another, and then close them in a third loop? Why not, in a single loop, open a file, read from it, and close it? There is a limit to the number of file descriptors a process may hold; I'm hoping you're not hitting it...



For us to really help you, tell us what you want to do.
mark
Quote:Original post by Oluseyi
First, what do you mean by "doesn't work"? What are you expecting and what are you experiencing?

Second, when using C-style strings (ie, null-terminated character arrays), you need to allocate one extra char for the null. map05.txt is 9 characters; you need 10 to represent it as a C-string. If your MAXNUMLEVELS is 2 or 3 digits, then you need even more space... Failing to format your string properly will break many other routines, so that may be the first of your problems.

Third, why do you open all the files in one loop, read from them in another, and then close them in a third loop? Why not, in a single loop, open a file, read from it, and close it? There is a limit to the number of file descriptors a process may hold; I'm hoping you're not hitting it...



For us to really help you, tell us what you want to do.


I am expecting my function that draws the tiles to work, but it doesn't draw anything. The screen remains blank. MAXNUMLEVELS is 4. I may bump it up to a double digits number but for now I'm leaving it at 4.

And regarding the loops I'm kinda rusty on C so I guess that was a simple oversight (wrong word you know what I mean).

Also I changed the 9 to 10 and that didn't change anything.

This might be a good time to get to know how to use your debugger.
I think the problem is that I don't really understand how fgetc() works.

I did a quick google search and found this:

fgetc() reads the next character from stream and returns
it as an unsigned char cast to an int, or EOF on end of
file or error.

So when it reads each '1' is fgetc() returning the char or the letter or something else? Not sure what I mean, hopefully you know what I mean though.
Hmm well I fixed it with some tinkering.

Namely:

g_levels.map[n][m] = fgetc(maps);


to

g_levels.map[n][m] = fgetc(maps) - 48;


Since the decimal value of the character '1' is 48.
Then just write '1'. It's more expressive.
Sure, everybody could figure out in a few moments 48 could be related to a special char you somehow decided to use as a reference but making it explicit isn't a bad idea.

Previously "Krohm"

change what exactly?
You can use the integer value of a letter or number by using 'x' ... i:e

int number;number = '1';  // <-- notice the ' 'number = 48;
"Game Maker For Life, probably never professional thou." =)

This topic is closed to new replies.

Advertisement