Thread: GetSaveFileName

  1. #1
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630

    GetSaveFileName

    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
                    .....
                }
           }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Did you try using a message box to debug? Have the text outputted to a message box and see exactly what is in there.

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Did that. Its all garbage after the call to GetSaveFileName

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Didnt change anything, and no it works all magically by itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doesn't write in file when call GetSaveFileName()
    By randall81 in forum Windows Programming
    Replies: 1
    Last Post: 03-28-2009, 01:34 PM
  2. GetSaveFileName exception
    By stanlvw in forum Windows Programming
    Replies: 4
    Last Post: 07-04-2008, 11:42 AM
  3. Major Problems with GetSaveFileName() and GetOpenFileName()
    By CodeHacker in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2004, 11:05 AM
  4. CFileDialog or GetSaveFileName()
    By jkw2068 in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2003, 09:38 AM