How to write string from EditControl to a text file in Visual Studio 2008 , using the “Save To..” dialog?
-
01-10-2019 - |
Question
I'm writing my first app in MFC, and I was looking to include a very simple feature : Include a "Save To File" button that on being clicked will bring up the familiar "Save As.." Dialog box and will ultimately save the data in a text file. I couldn't find how to invoke this dialog box - can someone just point me at the right way to do it ?
Solution
What you need is CFileDialog :
CFileDialog d(FALSE);
if(IDOK == d.DoModal())
{
CString sFileName = dlg.GetPathName();
// ... Save your text to the file
}
There are many parameters in the CFileDialog constructor that you can change to affect how the dialog behaves, check the MSDN documentation for details.
OTHER TIPS
For what you've described, you should probably just use a CEditView
instead of writing your own code. It's pre-written, tested, etc.
For most other situations, you should realize that MFC itself normally handles the details of creating the Save As...
dialog and such, so all you normally have to do is put code into your document class' Serialize()
member function to read and write your document's data. That's passed a reference to CArchive
object, so all you have to deal with is writing or reading the data for your document object.