简体   繁体   English

MFC保存文件对话框

[英]MFC Save file dialog

I am writing an MFC C++ application that has a Save As button for saving a .txt file to the disc. 我正在编写一个MFC C ++应用程序,它有一个“另存为”按钮,用于将.txt文件保存到光盘。 With it I am trying to add an extra verification for file overwriting (if a file with the same filename exists, then it should query the user if he wants to overwrite the old file or not). 有了它,我试图为文件覆盖添加额外的验证(如果存在具有相同文件名的文件,那么它应该查询用户是否要覆盖旧文件)。 I have tried this with the below code, but it doesn't really work. 我用下面的代码试过这个,但它确实不起作用。 When I click No on the MessageBox, it should reopen the Save As file dialog, but instead it gives me two errors: the first one is Debug assertion failed , and the second one is Encountered an improper argument . 当我在MessageBox上单击No时,它应该重新打开另存为文件对话框,但它会给我两个错误:第一个是Debug assertion failed ,第二个是Encountered an improper argument How should I do this better? 我该怎么做得更好? This is the code: 这是代码:

char strFilter[] = { "Text Files (*.txt)|*.txt|" }; 

    CFileDialog FileDlg(FALSE, CString(".txt"), NULL, 0, CString(strFilter)); 

    while(true)
    {
        if( FileDlg.DoModal() == IDOK ) // this is the line which gives the errors
        {
            agendaName = FileDlg.GetFileName(); //filename
            agendaPath = FileDlg.GetFolderPath(); //filepath (folders)

            if(model->agendaExists(CSToString(agendaPath+TEXT("\\")+agendaName))) // there is another file called the same way
            {
                if(MessageBox(TEXT("A file with the specified name already exists. Overwrite?"), TEXT("File exists"), MB_YESNO) != 6) // user clicked NO (do not overwrite file)
                {
                    continue;
                }

            }

            model->sendToFile(CSToString(agendaPath+TEXT("\\")+agendaName));  // the file is unique so the agenda named agendaName found at path agendaPath is saved
            return;
        }
    }

It should be mentioned that the errors occur on line 7 and only on the second loop through the while . 应当提到的是发生在管线7,并且只在通过所述第二环中的错误while

CFileDialog can detect itself if a file exists and prompt the user for overwriting. 如果文件存在,CFileDialog可以检测自身并提示用户覆盖。

explicit CFileDialog(
   BOOL bOpenFileDialog,
   LPCTSTR lpszDefExt = NULL,
   LPCTSTR lpszFileName = NULL,
   DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
   LPCTSTR lpszFilter = NULL,
   CWnd* pParentWnd = NULL,
   DWORD dwSize = 0
);

Just pass OFN_OVERWRITEPROMPT for the flags. 只需传递OFN_OVERWRITEPROMPT标志即可。

As for your problem, run in Debugger and when you get that assertion press the Retry button to see where the problem comes from (you'll probably have to look through the call stack also). 至于你的问题,在Debugger中运行,当你得到那个断言时,按Retry按钮查看问题的来源(你可能还需要查看调用堆栈)。 Maybe you should try putting this in the while loop: 也许你应该尝试把它放在while循环中:

CFileDialog FileDlg(FALSE, CString(".txt"), NULL, 0, CString(strFilter)); 

You should use the OFN_OVERWRITEPROMPT flag in the constructor. 您应该在构造函数中使用OFN_OVERWRITEPROMPT标志。 That flag is usually one of the default flags, but you have set your flags to 0. So, if you do: 该标志通常是默认标志之一,但您已将标志设置为0.因此,如果您执行以下操作:

CFileDialog FileDlg(FALSE, CString(".txt"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, CString(strFilter));

if (FileDlg.DoModal() == IDOK)  
{  
    model->sendToFile(CSToString(FileDlg.GetPathName()));
}

It should work. 它应该工作。 By the way, GetPathName() gets the full path to the selected file, so you don't need to get the folder and the file name in 2 steps. 顺便说一下, GetPathName()获取所选文件的完整路径,因此您无需分两步获取文件夹和文件名。

Try including below line inside the while loop (as first line in while loop) 尝试在while循环中包含下面的行(作为while循环中的第一行)

CFileDialog FileDlg(FALSE, CString(".txt"), NULL, 0, CString(strFilter));

This line is outside the while loop in your code 该行位于代码中的while循环之外

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

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