Link to home
Start Free TrialLog in
Avatar of lucki_luke
lucki_lukeFlag for Germany

asked on

CString Replace doesnt work

Hey,

I'm writing a program that reads a text in unix format (only \n) and then saves it again in unix format.
Now, so that my program can display it correctly I insert a \r before every \n.
But when I try to save it again, I want to remove those \r again.
I'm using an CString but Replace doesnt work somehow:
I tried several versions but none works

statusMessage.Replace('\r', NULL);

statusMessage.Replace("\r\n", "\n");

It also doesnt work when I try to replace a with b...

the var is filled here:
GetDlgItemText(hWnd, EDT_MESSAGE, statusMessage.GetBuffer(msgLen), msgLen + 1);

Also, when I tried to get the return value of the replace, it wouldnt give me anything, my declared variable wasnt even listed in the locals panel.
Btw, this stuff is all done in the MessageHandling function.

Can anyone help?

Regards,
lucki_luke
Avatar of Axter
Axter
Flag of United States of America image

How are you writing it to the file?
You should write to the file in binary format.
Avatar of lucki_luke

ASKER

In fact I'm writing to the registry.
>>statusMessage.Replace('\r', NULL);


Don't do the above, or it will terminate you're string.
You need to use the string version of replace, and not the character version.
statusMessage.Replace("\r", "");

The string version can replace a different size string, where as the character version will do a one for one swap.
Doesnt work either...
I dont think that its a problem with what I enter as arguments, I think its a problem with the Replace function itself or something with CString as it doesnt do anything no matter what I enter as arguments and it doesnt return a value. Its as if this line was just ignored.
Please post more of your code.

When you say it doesn't work, are you still seeing '\r' in the string, and how are you determining that the character is still there?
Ok,

Yes, I am still seeing the '\r' in the string. When I open regedit and go to the specified location, I can still see the \r in there because its a binary type. (There is always 0D 0A and it should only be 0A)

Here is the code that does anything with the string:

HWND hEditMsg = GetDlgItem(hWnd, EDT_MESSAGE);
int msgLen = (int)SendMessage(hEditMsg, WM_GETTEXTLENGTH, 0, 0);

CString statusMessage;

GetDlgItemText(hWnd, EDT_MESSAGE, statusMessage.GetBuffer(msgLen), msgLen + 1);
statusMessage.Replace("\r", "");

RegSetValueEx (hKey, (CString)"Description", 0L, REG_BINARY,(const BYTE *)(const char *)statusMessage, msgLen);

And if I try to see the result of the replace like so:

int nS = statusMessage.Replace("\r", "");
MessageBox(hWnd, (LPCTSTR)nS, "a", MB_OK);

I get an empty message box. But that occurs no matter what I try to replace, say replace "a" with "b" will still return the same result.

Oh well,

figured that out now....
Very stupid mistake of mine.

I forgot to call
statusMessage.ReleaseBuffer();

Anyway, Thanks for your help!
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial