0

I created a MFC programm with a menu option to Save a file. When I click it it shows the CFileDialog and I can choose the location where I want to save my file. When I Click save in the Dialog, it closes the dialog, but after that it does nothing. AND it didn't save my file. What am I doing wrong?

Here is the code

CFileDialog *dlg = new CFileDialog(FALSE, L"dr", NULL, NULL,
                   L"Drawings (*.dr)|*.dr|"
                   L"All Files||");

bool result = dlg->DoModal();

if(result)
{
    MessageBox(0, dlg->GetPathName(), L"Draw", 0);
}

The bool result, is purely there to check if there is no problem/error.

willemjan92
  • 57
  • 1
  • 11

2 Answers2

2

The file save dialog is called "file save dialog" because its caption says "Save File" and it allows you to only select a single file. That does not mean that it actually does any saving of files. It just returns to you the filename selected by the user. You are still responsible for writing the code which will save your file using this filename.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • 1
    Thank you! didn't know that.. When I googled the code above was in almost all the answers and because it is my first time saving with MFC, I thought that it should work. – willemjan92 Feb 11 '15 at 15:10
  • Side note: You can use the `CFileDialog` to select multiple files, if you pass the right flags to it. – sergiol Apr 23 '17 at 00:45
1

The CFileDialog does not save the file for you, it only provides you with a dialog for the user to determine where (and if!) the file should be saved. The return value of DoModal() should be compared to IDOK before proceeding to save. From the dialog's member functions you can get the path and filename the user selected. With that, you can create/open a file and save your data.

Henk Kok
  • 191
  • 2
  • 11