-
November 5th, 2013, 04:02 PM
#1
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
-
November 5th, 2013, 04:26 PM
#2
Re: could not save file using cfiledialog Saveas in MFC
Try using GetPathName() instead of GetFileName().
-
November 5th, 2013, 05:01 PM
#3
Re: could not save file using cfiledialog Saveas in MFC
It worked, thanks for the suggestion GCDEF
-
November 5th, 2013, 05:05 PM
#4
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 12th, 2013, 09:06 AM
#5
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|