Sign in to follow this  

fopen problem

Recommended Posts

I wrote a level load function that worked fine in debug, but when I tried to make a release exe it crashes. I've tracked the problem to the opening of the file. here is the code, given a level number it will open a file in the Level folder
	FILE * pFile;
	char filename[] = "Levels/level";	//first part
	char num[2];
	wsprintf(num, "%d", level);	//level num
	strcat(filename, num);		//add # to level
	strcat(filename, ".dat");	//add .dat to level

	if( (pFile = fopen (filename,"r")) != NULL)
	     return pFile;

This should open a file named "level1.dat" for example I replaced the string concatenating code with
char filename[] = "Levels/level1.dat";

and it works fine, so I believe the problem is in putting the string together. btw the function returns a file pointer so another function can read in data. I'm not sure why it works in debug, but not release. The error I get when trying to run the program us "Exception privileged instruction (0x0000096) at 0x00400080" any ideas?

Share this post

Link to post
Share on other sites
It looks like a stack overrun when using strcat() to append to filename. You haven't allocated enough room for filename. Increase the size of the filename array.

Share this post

Link to post
Share on other sites
Is there any particular reason why you aren't just doing the following?

wsprintf(szBuffer, "Levels/level%d.dat", iLevel);

Of course, provided that szBuffer is large enough to accommodate that string...

If you're using C++, you might want to use the STL.

Share this post

Link to post
Share on other sites
Original post by localrob2
WOW excellent, thanks.

What gave you that idea I mean,what hinted to an overrun stack?

filename is an array containing 13 characters; 12 characters plus null terminator. The array is filled with the string specified with no more space left. Then you append a number and file extension, and since the array is full to begin with, these must end up outside the array.

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