Delete [] charPtr... Pls help
-
c121hains wrote: string1 still equals "this is a string \r\n" and doesn't append string2 How are you determining this? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Come quietly or there will be... trouble.
-
In the debugger.. There is no change to the original string: originalString = "the original string \r\n"; originalString += "a new string \r\n"; ------ originalString still equals "the original string \r\n"; Do you get a different result?
It should work. Maybe the debugger is showing only the first line. Try doing a TRACE() and see the result in the output window. -- jlr http://jlamas.blogspot.com/[^]
-
c121hains wrote: All i want to do is get all of the text together from an array of CString objects and put it into a single char* pointing to a string. Try this:
void MergeLines(const CString* lines, int nLines, CString& result)
{
// Get total length
int nTotalLength = 1; // at least a null terminator will be needed
for (int i = 0; i < nLines; i++)
nTotalLength += lines[i].GetLength();
result.Empty();
// Avoid re-allocating and copying in each iteration
result.GetBuffer(nTotalLength);
result.ReleaseBuffer();
// Concatenate all strings into result
for (int i = 0; i < nLines; i++)
result += lines[i];
}Disclaimer: I didn't compile it. May contain some error[s]. -- jlr http://jlamas.blogspot.com/[^]
This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?
-
It should work. Maybe the debugger is showing only the first line. Try doing a TRACE() and see the result in the output window. -- jlr http://jlamas.blogspot.com/[^]
This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?
-
c121hains wrote: ...and doesn't append string2!! How do you know?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?
-
c121hains wrote: string1 still equals "this is a string \r\n" and doesn't append string2 How are you determining this? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Come quietly or there will be... trouble.
This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?
-
This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?
c121hains wrote: This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; [...] return allText.GetBuffer(0); That may seem to work, but it actually doesn't. You can't return a pointer to the internal buffer in
allText
, since that variable will be destroyed as soon as the function terminates. Check the code I suggested in a previous post (the function receives aCString
reference from the caller and stores the result in it). c121hains wrote: Any idea why I need to use GetBuffer instead of string1 += string2? You don't need to useGetBuffer
; that's not the problem. -- jlr http://jlamas.blogspot.com/[^] -
This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?
c121hains wrote: Any idea why I need to use GetBuffer instead of string1 += string2? Ok. I already answered this. :) Again, I suggest you try the implementation I posted before. -- jlr http://jlamas.blogspot.com/[^]
-
c121hains wrote: This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; [...] return allText.GetBuffer(0); That may seem to work, but it actually doesn't. You can't return a pointer to the internal buffer in
allText
, since that variable will be destroyed as soon as the function terminates. Check the code I suggested in a previous post (the function receives aCString
reference from the caller and stores the result in it). c121hains wrote: Any idea why I need to use GetBuffer instead of string1 += string2? You don't need to useGetBuffer
; that's not the problem. -- jlr http://jlamas.blogspot.com/[^] -
This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?
Well, that only "works" by chance. You're returning a pointer to the local variable
allText
which immediately goes out of scope. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | NEW~! CP SearchBar v3.0 | C++ Forum FAQ I even hear the Windows "OMG I booted up fine" sound. -- Paul Watson diagnosing hardware problems.