Advertisement Jump to content


This topic is now archived and is closed to further replies.


error with GetOpenFileName

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

Hello to everyone. I've written a post last Friday but got no response. I did some tricks to remove my bug but they didn't catch so here goes one more try: In my program I need to save a trace file of the mouse events. All this is good. Last Friday I needed to add a file open operation so the user can select which level to load. What better I thought than to use window's own file open dialog (used it last week in another project, no trouble). This is the function I call to get the file name from the user:
void CGame::OpenFile()
	char szFile[MAX_PATH];
	//const short int maxPath = 260;

	char currDirectory[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, currDirectory);
	strcat(currDirectory, "\\Levels");

	ZeroMemory(&file, sizeof(file));
	file.lStructSize = sizeof(file);
	file.hwndOwner = NULL;
	file.lpstrFile = szFile;

	file.lpstrFile[0] = '\0';
	file.nMaxFile = sizeof(szFile);
	file.lpstrFilter = "Coucou Cache Level\0*.lvl\0All\0*.*\0";
	file.nFilterIndex = 1;
	file.lpstrFileTitle = NULL;
	file.nMaxFileTitle = 0;
	file.lpstrInitialDir = currDirectory;

	if (GetOpenFileName(&file)) {
		m_cCurrentLevelFilename = szFile;
The problem is that if the user presses ok in the file open dialog the trace file won't get saved. In fact I tried the following change in the above code:
if (GetOpenFileName(&file)) {
       m_cCurrentLevelFilename = "Levels\\default.lvl";
I run the program two times (no changes to the source code). The first time, I selected a level (I know the selected file name is ignored but bear with me) pressed ok and my program could not load the level. Second run, I pressed escape and the "default" level loaded fine. I suppose this is the same reason why the trace file is not saved. So does anyone know why GetOpenFileName messes up with my fstreams? Any help appreciated. edit: This is an example how I open my files:, ios_base::in | ios_base::binary);
[edited by - moucard on June 7, 2004 6:19:09 AM]

Share this post

Link to post
Share on other sites
Finally got the solution. The GetOpenFileName changed the default directory if the call was succesful. No wonder I couldn''t save my file afterwards! The OFN_NOCHANGEDIR flag should be set. Crappy documentation Microsoft! Should you write somewhere in the help files that this functions changes the directory?

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!