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 06:22 AM. Reason: added CODE tags

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

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,393

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,023

    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 Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: 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 is a CodeGuru survey question.


Featured


HTML5 Development Center