void Copendlg::onsave ()
{
CString strpath,strtext= "";
Char write[30000];
OpenFileName ofn;
ZeroMemory (&ofn,sizeof (ofn));
Ofn.lstructsize=sizeof (OFN);
Ofn.hwndowner=this->getsafehwnd ();
Ofn.lpstrfilter= "All Files (*.txt) \0*.txt\0\0";
Ofn.lpstrcustomfilter=null;
ofn.nfilterindex=0;
Char filename[128];
Filename[0]= ' ";
Ofn.lpstrfile=filename;
ofn.nmaxfile=128;
Ofn.lpstrfiletitle=null;
Ofn.lpstrinitialdir=null;
ofn.lpstrtitle= "Save text file";
Ofn. Flags=ofn_filemustexist| ofn_hidereadonly| Ofn_overwriteprompt;
Ofn.lpstrdefext=null;
if (GetSaveFileName (&ofn) ==0)
Return
Strpath=filename;
if (Strpath.right (4)!= ". txt")
strpath+= ". txt";
CFile file (_t (strpath), cfile::modecreate| Cfile::modewrite);
M_edit.getwindowtext (StrText);
strcpy (Write,strtext);
File. Write (Write,strtext.getlength ());
File. Close ();
MessageBox ("Save complete!", "Save Prompt");
}
The operation effect is as follows:
If the file already exists and the duplicate name is saved, prompt for replacement.
Add: ofn_overwriteprompt only have file name replacement prompts. After the replacement, the original file content is none, to save the latest content.
Ofn_overwriteprompt (from MSDN)
Causes the Save as dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.