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]);
}
}
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.
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.
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.