title: MFC C++: How to actually save a certain file to the system, not just open a save as dialog box
body: <p>I searched everywhere but I can't find sample on how to actually save a file to the system. Threads about opening a Save File dialog box can be read in numerous sites but the successful saving of the user created file to a user selected path is always cut (//add your code here). Please bear with me as I am new in C++ (MFC).</p>
  <p>I know I need to actually code the saving of the data to the file path but I just don't know how.</p>
  <p>Code snippet (via CFileDialog):</p>
  <pre><code>void CTryDlg::OnBnClickedSaveAs()
  {
      CFileDialog dlg(FALSE);
      dlg.m_ofn.nMaxFile = MAX_PATH;
      dlg.m_ofn.lpstrFilter = _T(&quot;Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0&quot;);
      dlg.m_ofn.lpstrTitle = _T(&quot;Save File As&quot;);
      CString filename;
      if (dlg.DoModal() == IDOK)
      {
          filename = dlg.GetPathName(); // return full path and filename
          //write your sample code here to save the file to the user selected path
      }                                                                                           
  }
  </code></pre>
  <p>Code snippet via GetSaveFileName():</p>
  <pre><code>OPENFILENAME SfnInit()
  {
      OPENFILENAME t_sfn;
  
      char szFileName[MAX_PATH] = &quot;&quot;;
      ZeroMemory(&amp;t_sfn, sizeof(t_sfn));
      t_sfn.lStructSize = sizeof(t_sfn);
      t_sfn.hwndOwner = NULL;
      t_sfn.lpstrFilter = _T(&quot;Text file\0*.txt\0&quot;);
      t_sfn.lpstrFile = szFileName;
      t_sfn.lpstrTitle = _T(&quot;Save As\0&quot;);
      t_sfn.nMaxFile = MAX_PATH;
      t_sfn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
      t_sfn.lpstrDefExt = _T(&quot;Text file\0*.txt\0&quot;);
  
      if (GetSaveFileName(&amp;t_sfn2) != true)
      {
          AfxMessageBox(_T(&quot;Saving file canceled!&quot;));
      }
      else
      {
          //write your sample code here to save the file to the user selected path
      }
  }
  </code></pre>
  <p>Anybody who can provide a very simple sample code that could actually save a user desired file (ex: text file) to the user selected path will be greatly appreciated.
  I have also read that the program should run as administrator.</p>
  <p>Thank you.</p>