VC file Action: Save file dialog box OpenFileName code

Source: Internet
Author: User
Tags save file


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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.