fopen problem

Started by
4 comments, last by Brother Bob 18 years, 1 month ago
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?
Advertisement
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.
WOW excellent, thanks.

What gave you that idea I mean,what hinted to an overrun stack?
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...

P.S.
If you're using C++, you might want to use the STL.
Yes I could have used that too, but I didn't know that before. This was my first attempt at file i/o. so I'm still learning things.
Thanks for the help
Quote: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.

This topic is closed to new replies.

Advertisement