Ok, I dont understand why this doenst work. I have a buffer, szFile which holds the file name. and pass that to my OPENFILENAME struct. The dialog opens when I call GetSaveFileName and shows the desired name, but if i change the name of the file and press save and try to access the lpstrFile after the call to GetSaveFIleName the program causes an error.
Code:
static ::OPENFILENAME ofn;
static char szFilter[] = "Text Files (*.TXT)\0*.txt\0";
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = hWnd ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = szFilter ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.lpstrFile = NULL ;
ofn.nMaxFile = _MAX_PATH ;
ofn.lpstrFileTitle = NULL ;
ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = "Save Output As:" ;
ofn.Flags = 0 ;
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = "txt" ;
ofn.lCustData = 0L ;
ofn.lpfnHook = NULL ;
char szFile[] = "Output.txt";
ofn.lpstrFile = szFile;
ofstream out;
if(GetSaveFileName(&ofn))
{ if(strcmp(szFile,ofn.lpstrFile)) // this fails even if i select output.txt as name bc lpstrFile is null now?
{ out.open(szFile,ios::out);
....
}
else
{ out.open(ofn.lpstrFile,ios::out); // this causes prog to crash
.....
}
}