Jump to content
  • Advertisement
Sign in to follow this  
always_learning

don't understand basic file I/O

This topic is 3716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
You can use the integer value of a letter or number by using 'x' ... i:e


int number;
number = '1'; // <-- notice the ' '
number = 48;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!