could not save file using cfiledialog Saveas in MFC
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: could not save file using cfiledialog Saveas in MFC

  1. #1
    Join Date
    Nov 2013
    Posts
    2

    could not save file using cfiledialog Saveas in MFC

    Hi everyone,

    I am trying to develop a GUI using MFC, but I am having trouble using CFiledialog to save a file. The problem is, the file is not getting saved to the folder when I use the CFiledialog. Below is the code I am using.

    Code:
    CString szFilter = "XNRep Files (*.xnrep)|*.xnrep||";
    CString s = "xnrep";
    CString t = "";
    CFileDialog fileDlg(FALSE, s, t, NULL, szFilter);
    if(fileDlg.DoModal() == IDOK)
    {
    	std::ofstream file;
    	file.open(fileDlg.GetFileName(),std::ios::out);
    	if (file !=  NULL)
    	{
    		file << (*assembly);
    		file.close();
    	}
    }
    After the file dialog opens up, I enter the name of the file and select OK button. But the file does not show up in the directory I am saving to. Any suggestions would be appreciated. Thanks.
    Last edited by ovidiucucu; November 12th, 2013 at 07:22 AM. Reason: added CODE tags

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,507

    Re: could not save file using cfiledialog Saveas in MFC

    Try using GetPathName() instead of GetFileName().

  3. #3
    Join Date
    Nov 2013
    Posts
    2

    Re: could not save file using cfiledialog Saveas in MFC

    It worked, thanks for the suggestion GCDEF

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,872

    Re: could not save file using cfiledialog Saveas in MFC

    Before posting, please format you code and use code tags. Go Advanced, select the code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,187

    Re: could not save file using cfiledialog Saveas in MFC

    An additional note:
    Once you are using MFC, it has no much sense to mix STL stuff if it's not necessary.
    MFC has its own classes for dealing with files, like CFile and CStdioFile.

    Example
    Code:
        CString strFilter = _T("XNRep Files (*.xnrep)|*.xnrep||");
        CString strDefExt = _T("xnrep");
        CString strToWrite = _T("Baba Safta");
    
        CFileDialog dlgFile(FALSE, strDefExt, NULL, NULL, strFilter);
        if(IDOK == dlgFile.DoModal())
        {
            CString strPathName = dlgFile.GetPathName();
            try
            {
                CStdioFile file(strPathName, CFile::modeWrite|CFile::modeCreate);
                file.WriteString(strToWrite);
            }
            catch(CException* e)
            {
                e->ReportError(); // show what's going wrong
                e->Delete();
            }
        }
    That way, it's much easier to handle UNICODE/ANSI strings, avoid leaks, handle errors, and last but not the least prevent headaches.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)