【发布时间】:2011-08-23 22:20:50
【问题描述】:
我正在编写一个 MFC C++ 应用程序,该应用程序有一个“另存为”按钮,用于将 .txt 文件保存到光盘。有了它,我正在尝试为文件覆盖添加额外的验证(如果存在具有相同文件名的文件,那么它应该询问用户是否要覆盖旧文件)。我已经用下面的代码试过了,但它并没有真正起作用。当我单击消息框上的否时,它应该重新打开另存为文件对话框,但它给了我两个错误:第一个是Debug assertion failed,第二个是Encountered an improper argument。我应该如何更好地做到这一点?这是代码:
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;
}
}
应该提到,错误发生在第 7 行,并且仅在通过while 的第二个循环中发生。
【问题讨论】: