?
Solved

CString Replace doesnt work

Posted on 2006-05-20
10
Medium Priority
?
743 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:lucki_luke
  • 4
  • 4
9 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 16724973
How are you writing it to the file?
0
 
LVL 30

Expert Comment

by:Axter
ID: 16724978
You should write to the file in binary format.
0
 
LVL 9

Author Comment

by:lucki_luke
ID: 16725030
In fact I'm writing to the registry.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Expert Comment

by:Axter
ID: 16725044
>>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.
0
 
LVL 9

Author Comment

by:lucki_luke
ID: 16725063
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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16725187
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?
0
 
LVL 9

Author Comment

by:lucki_luke
ID: 16725638
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.

0
 
LVL 9

Author Comment

by:lucki_luke
ID: 16725669
Oh well,

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

I forgot to call
statusMessage.ReleaseBuffer();

Anyway, Thanks for your help!
0
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16753539
Closed, 125 points refunded.

GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question