saving and opening

Started by
1 comment, last by water908 20 years, 10 months ago
I know how to save and read in files, but I want to know how to be able to search for where I want to save and read in from. What I mean is if you use Word and you want to save you can choose what folder it is saved in and it''s name, and have that little window, if possible. Thank you for your help.
Advertisement
What you want is the "open" and "save as" common dialog boxes. Here are the two in Win32 API.

Open...
void Open_Game(HWND hwnd){	OPENFILENAME ofn;	char szFileName[MAX_PATH] = "";	ZeroMemory(&ofn, sizeof(ofn));	ofn.lStructSize = sizeof(OPENFILENAME);	ofn.hwndOwner = hwnd;	ofn.lpstrFilter = "Some file (*.huh)\0*.huh\0";	ofn.lpstrFile = szFileName;	ofn.nMaxFile = MAX_PATH;	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;	ofn.lpstrDefExt = "huh";	if(GetOpenFileName(&ofn))	{           //Activate your reading function here, pass in ''szFileName'' for the "location of the file"        }}

Change what file extension you want by replacing the *.huh stuff.
The ofn.lpstrDefExt you insert in a generic file extension. Say user enters in the correct file name but not correct extension. If it does not match the filter... try to match it to the ofn.lpstrDefExt before exiting.
The szFileName contains the path of the file you want to open, pass into fstream or something of that nature.

void Save_Game(HWND hwnd){	HANDLE hFile;	OPENFILENAME ofn;	char szFileName[MAX_PATH] = "";	ZeroMemory(&ofn, sizeof(ofn));	ofn.lStructSize = sizeof(ofn);	ofn.hwndOwner = hwnd;	ofn.lpstrFilter = "Some file (*.huh)\0*.huh\0";	ofn.lpstrFile = szFileName;	ofn.nMaxFile = MAX_PATH;	ofn.lpstrDefExt = "huh";	ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;	if(GetSaveFileName(&ofn))	{          //File save function here        }        else        {          //Error box thing, file cannot be saved here        }

Practically same thing as above.

I suppose just cut and paste if you want to utilize the common dialog boxes, just make sure to adjust for variable differences.

If you have questions, ask me and I''m sure either the others or myself can answer them. Hope that helps.
Thanks, I''ll try it out tomorrow.

This topic is closed to new replies.

Advertisement