views:

1148

answers:

7

How can you put escape characters (like newline character -- \n) to a CString?

A: 

Do you mean an unescaped version? If so then just escape the escape

CString("\\n");
JaredPar
not that. "\n" -if I do that it appears as a garbage character.
A: 

The newline character is '\n' in single quotes. Just add that to your cstring.

Perchik
\n becomes a box (garbage character). it doesn't insert a new line.
+4  A: 

I think your problem is not inserting a newline in the CString, but in the method that you are using to display the string

Maurice Perry
+4  A: 

\n becomes a box (garbage character). it doesn't insert a new line.

Is this what you find in the debugger? If so, this is okay. The newline character has a hex value of 0xA or 10 in decimal. Since this is a non-printable character, that's what the debugger will show you.

Apart from that, if you are using notepad to view the output, it may not come out right. Try "\r\n" in order to get notepad to split correctly.

dirkgently
+1  A: 

When I was a younger coder I had the same problem when writing to a .txt file on windows and opening it in notepad. The newline characters show up as these squares. If you want the appearance of newlines in notepad use "\r". This isn't really a solution, but I have an intuition that this is your problem...

cpatrick
A: 

CString strMessage = "";

strMessage.Format("Alpha \n Beta");

"\n" appears as box character in debugger but while displaying on the UI it displays properly.

Vinay
A: 

thanx..Patric :)

a good user