简体   繁体   English

C ++保存对话框

[英]C++ Save dialog

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. 我调用一个保存对话框来保存xml文件(例如:a.xml),然后将其副本(例如:a_clone.xml)静默保存到其他位置。 But it only work for a.xml file. 但是它仅适用于a.xml文件。 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! 谁能帮我!

CopyFile is a simple function and generally reliable. CopyFile是一个简单的函数,通常是可靠的。 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. std::ofstream myfile相同,您可能没有检查错误。

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. 注意,“ c:\\ program files”等是受保护的目录,您需要管理员权限才能复制到这些目录。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM