You probably open output file as text mode to save RE content. You can try to open it as binary when saving (and loading, too, of course).
Technical explanation: You get RE content from control with end of line chars intact, that is, 0x0d 0x0a ("\r\n") which are used by MS OSes as being end of line marker. When saving, RTL routine replaces all 0x0a ('\n') chars with 0x0d 0x0a ("\r\n"), then end of lines have become 0x0d 0x0d 0x0a ("\r\r\n"). When opening this file by VC++ editor, it complains for these unexpected EOL markers, then interprets them double end of lines.
EDIT:
You aren't opening the file as binary mode. You should specify CFile::typeBinary flag. If you don't specify it, the default mode is typeText for CStdioFile.
CFileDialog dlg(FALSE, NULL, NULL, OFN_OVERWRITEPROMPT,
L"C++ Source Files (*.cpp)|*.cpp|C++ Header Files (*.h)|*.h|");
CStdioFile file;
CString buffer;
CString textfile;
if(dlg.DoModal() == IDOK)
{
file.Open(dlg.GetPathName(),
CFile::typeBinary | CStdioFile::modeCreate | CStdioFile::modeWrite);
text.GetWindowText(buffer);
file.Write((void*)(LPCTSTR)buffer, buffer.GetLength() * sizeof(TCHAR));
file.Close();
}
You aren't doing anything for UNICODE conversion with above code. If you didn't change project settings to MBCS, saved file should still be UNICODE.