Jump to content
  • Advertisement
Sign in to follow this  
ursus

File rw and win32

This topic is 4506 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

Coders, I stumbled across something I don’t really understand. Let me just quickly present a bit of code:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	FILE *log;

	switch(msg)
	{
		case WM_CREATE:
		{
			log = fopen("log.txt", "a");
			fprintf (log, "Program started");
		}
		break;
		
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		
		case WM_DESTROY:
		{	
			fprintf (log, "Program closed");
			fclose (log);
						
			PostQuitMessage(0);
		}
		break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}


Just a standard WinProc followed by a standard WinMain. For some reason when I close the window – apparently when it comes to the file manipulation lines the program crashes. Any suggestions on what I am doing wrong? Tons of thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Your FILE* log is inside the scope of the WndProc. For every message your WndProc receives a new FILE* log is created on the stack and discarded on leaving the function.

So on WM_DESTROY you're trying to write to an uninitialized FILE* handle.

Either have A) a global FILE* log somewhere, B) make it static FILE* log or C) have a logging class that takes care of the file handle and stays alive outside the WndProc function.

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!