CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: GetSaveFileName

  1. #1
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    GetSaveFileName

    hi, GetSaveFileName retuns a string without extension.
    is there a way i can make it add the extension to the filename?
    the filter is ok. i define it like this:

    Code:
        		CStringT::ConstDataT ExportFilterList[] = {
        			S("Scene Editor Object (*.seo)"), S("*.seo"),
        			S("3D Studio Max Object (*.3ds)"), S("*.3ds"),
        			S("Blender Object (*.blend)"), S("*.blend"),
        			S("Milkshape3D Object (*.m3d)"), S("*.m3d"),
        			S("\0")};
    constdatat is either char const* or wchar_t const *

  2. #2
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: GetSaveFileName

    I don't think you need the filter as an array, did you try defining the filter like:
    Code:
    char filter* = "File1(.ext1);*.ext1;File2(.ext2);*.ext2;*.*\0";
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  3. #3
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    389

    Re: GetSaveFileName

    The lpstrDefExt member of the OPENFILENAME structure gives you the ability to append an extension if the user doesn't type one.

  4. #4
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: GetSaveFileName

    thanks kkez! i will try.

  5. #5
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: GetSaveFileName

    this is not what im looking for... it must add the extension from the filter...

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: GetSaveFileName

    Can you post your code? How are you calling GetSaveFileName + how are you accessing the selected filename? The lpstrFile member of OPENFILENAME contains drive designator, path, file name, and extension of the selected file.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: GetSaveFileName

    Code:
        		const CStringT::ConstDataT ObjectFilterList[] = {
        			S("Scene Editor Object (*.seo)"), S("*.seo"),
        			S("3D Studio Max Object (*.3ds)"), S("*.3ds"),
        			S("Blender Object (*.blend)"), S("*.blend"),
        			S("Milkshape3D Object (*.m3d)"), S("*.m3d"),
        			S("\0")};
    
    
    
            CStringT SaveFileBox(CStringT::ConstDataT p_Filter = S("All (*.*)\0*.*\0"), CStringT::ConstDataT p_Directory = CApp::Directory(), DWORD p_Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT)
            {
            	CStringT l_FileName(MAX_PATH + 1);
            	OPENFILENAME l_File = {0};
            	l_File.lStructSize = sizeof(l_File);
            	l_File.hwndOwner = Handle();
            	l_File.lpstrFilter = p_Filter;
            	l_File.lpstrFile = l_FileName;
            	l_File.nMaxFile = l_FileName.Capacity();
            	l_File.lpstrInitialDir = p_Directory;
            	l_File.Flags = p_Flags;
            	::GetSaveFileName(&l_File);
            	l_FileName.SetSize(l_FileName.Capacity());
            	l_FileName.Depilate(0);
            	return(l_FileName);
            }
    
        	void FMainT::MMainT::MFileT::IExportT::OnClick()
        	{
    			CStringT l_File  = FMain.SaveFileBox(*ObjectFilterList);
    // save file
        	}
    i printed l_File and it did NOT have extension
    Last edited by Mitsukai; April 22nd, 2007 at 04:13 PM.

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: GetSaveFileName

    The problem is with "l_File.lpstrFilter = p_Filter;". The filter requires embedded \0 characters and that line will truncate the filter at the first \0 character.
    To be sure try to replace that line with:
    Code:
    l_File.lpstrFilter = S("Scene Editor Object (*.seo)\0*.seo\0")
    			S("3D Studio Max Object (*.3ds)\0*.3ds\0")
    			S("Blender Object (*.blend)\0*.blend\0")
    			S("Milkshape3D Object (*.m3d)\0*.m3d\0")
    			S("\0");
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  9. #9
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: GetSaveFileName

    litterals end with NULL character so my code is not wrong.

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: GetSaveFileName

    IIRC, you can only specify one default extension. If you want to use the extension that is selected in the filter, you'll have to do a little work on your own (like a custom save dialog).

    Viggy

  11. #11
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: GetSaveFileName

    When GetSaveFileName returns it will put the filter that has been selected by the user in OPENFILENAME::nFilterIndex. You can use that index to figure out the selected filter, then check whether the extension is already in the OPENFILENAME::lpstrFile and if not, add the extension yourself.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  12. #12
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: GetSaveFileName

    thanks, i could use _splitpath to find if there is an extension or not and append the right one from the filter if there is not one, thanks

  13. #13
    Join Date
    Feb 2002
    Posts
    4,640

    Re: GetSaveFileName

    That's easier then what I suggested.

    Viggy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured