Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm making a text editor for C++ using rich edit control.
I use CFileDialog to save the code in Rich edit to a *.cpp or *.h file.
When I save it and then open it with Visual C++ environment, it shows me Line endings message and it puts an empty line between every line of code.

How can I fix it?

Thanks in advance!
Posted
Updated 18-Mar-11 12:15pm
v2
Comments
Dalek Dave 18-Mar-11 18:15pm    
Edited for Grammar.

I'd rather drop the rich edit control and use something like:
Using Scintilla for syntax coloring in MFC[^]

It will save you a lot of time, and add a number of nifty features :)

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Mar-11 15:15pm    
Scintilla is the must-know thing is one looks to select and editor. Still, it has some problems.
As those problems are not your fault :-) and there are not many good options :-) you got 5.

Wait a minute... is there Scintilla for C++?
--SA
Dalek Dave 18-Mar-11 18:16pm    
Good Call.
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(), // more appropriate instead of GetFileName()
      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.
 
Share this answer
 
v2
Comments
George Findulov 18-Mar-11 6:12am    
When i make it binary, my program reads it the way it should, but VC++ can't read it normally.
Ozer Karaagac 18-Mar-11 6:18am    
Do you build your project using UNICODE character set?
George Findulov 18-Mar-11 6:23am    
yeah, is that a problem?
Ozer Karaagac 18-Mar-11 6:31am    
Normally you don't have any problem with it. Try open the file with notepad or any other text editor that supports UNICODE. I think, VC++ editor doesn't support UNICODE text files. If you want to save it as ANSI, you need to convert it before saving (maybe vise versa when loading).
George Findulov 18-Mar-11 6:39am    
Notepad opens it normally. Anyway how can i convert unicode to ASCII and the result should stay in letters, not the numbers - like (int)myLetter. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900