• Advertisement
Sign in to follow this  

Win32: GetSaveFileName doesn't like fstream?

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

Here's a tid bit of a code from my program that is executed on WM_LBUTTONUP:
bool SaveProfile(HWND hwnd)
{    
 OPENFILENAME SaveInfo;
 //fstream File;
 int No=ImageInfo.GetNumber(); 
 char Filename[256]; 
 char szFilter[256];
 
 //create the filter for the file
 strcat(szFilter, "Profile (*.mrp) ");
 strcat(szFilter, "*.mrp");
 szFilter[15]='\0';  
 szFilter[22]='\0';
 szFilter[23]='\0'; 

 //reset filename
 Filename[0]='\0';
 
 SaveInfo.lStructSize = sizeof(OPENFILENAME); 
 SaveInfo.hwndOwner = hwnd; 
 SaveInfo.lpstrFilter = szFilter; 
 SaveInfo.lpstrCustomFilter = NULL;
 SaveInfo.nFilterIndex = 0;
 SaveInfo.lpstrFile= Filename; 
 SaveInfo.nMaxFile = 100; 
 SaveInfo.lpstrFileTitle = NULL; 
 SaveInfo.nMaxFileTitle = 0; 
 SaveInfo.lpstrInitialDir = NULL; 
 SaveInfo.Flags = OFN_OVERWRITEPROMPT; 
 SaveInfo.lpstrTitle = NULL;

 GetSaveFileName(&SaveInfo);

 return 1;   
}


It compiles fine and works. Now, see that //fstream File; on the top? If I unqoute it (this declaring an fstream variable there), the program crashes whenever it gets to GetSaveFileName(&SaveInfo) giving the "there was a problem with this application and it will be closed. We are sorry for the inconvenice." Can anyone explain to me why this is happening? Another odd thing is, if I put my Display function (draw a bunch of stuff to the screen) just before calling this one, it will not crash at all... I am really confused right now :/

Share this post


Link to post
Share on other sites
Advertisement
The thing that jumps out at me is that you are strcat'ing into szFilter without initializing it first. strcat depends there already being a valid string in place.

Edit: Also your filter string is just plain wrong. It should look like

"Profile\0*.mrp\0\0"

Share this post


Link to post
Share on other sites
Alright, I seem to have fixed the problem by copying the exact thing they have in MSDN for opening files. Here's what it looks like, just fyi:


bool SaveProfile(HWND hwnd)
{
OPENFILENAME SaveInfo;
char Filename[256];

//reset filename
Filename[0]='\0';

ZeroMemory(&SaveInfo, sizeof(SaveInfo));
SaveInfo.lStructSize = sizeof(SaveInfo);
SaveInfo.hwndOwner = hwnd;
SaveInfo.lpstrFile = Filename;
SaveInfo.nMaxFile = sizeof(Filename);
SaveInfo.lpstrFilter = "Profile\0*.mrp\0\0";
SaveInfo.nFilterIndex = 1;
SaveInfo.lpstrFileTitle = NULL;
SaveInfo.nMaxFileTitle = 0;
SaveInfo.lpstrInitialDir = NULL;
SaveInfo.Flags = OFN_OVERWRITEPROMPT;

GetSaveFileName(&SaveInfo);


return 1;
}



Why did it do such bizzarre things beforehand is beyond me...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement