C++ Save dialog

134 views Asked by At

I call a save dialog to save a xml file (ex: a.xml), then save a clone of it(ex: a_clone.xml) to other location silently. But it only work for a.xml file. This my code for save dialog:

string Savefilename(char *filter = "Mission Files (*.mmf)\0*.mmf", HWND owner = NULL){
    OPENFILENAME ofn;
    char fileName[MAX_PATH] = "";
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = owner;
    ofn.lpstrFilter = filter;
    ofn.lpstrFile = fileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
    ofn.lpstrDefExt = "";
    ofn.lpstrInitialDir ="Missions\\";

    string fileNameStr;
    if ( GetSaveFileName(&ofn) )
    fileNameStr = fileName;

    return fileNameStr;
}

Can anyone help me please!

1

There are 1 answers

1
Barmak Shemirani On

CopyFile is a simple function and generally reliable. It fails here probably because target directory does not exit, or because you don't have write access to target directory. It's the same with std::ofstream myfile you are probably not checking for errors.

Make sure target directory exists. Give the full pathname to destination file. Make sure you have access to target directory.

Note, "c:\program files" etc. are protected directories, you need admin access to copy to these directories.

You can also check for errors:

SetLastError(0);

CopyFile(source, destination, FALSE);

DWORD error = GetLastError();
if (error)
{
    char buf[255];
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, error, 0, buf, 255, 0);
    cout << "error: " << error << ", " << buf << endl;
}