|
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. I'm doing memory allocation...
Why are you wanting to do this? The whole reason for using CString is so that you would not need to mess with char variables and memory allocation/cleanup.
c121hains wrote:
int sizeAllText = strlen(lines[0].GetBuffer(0));
strcpy(source, lines[0].GetBuffer(0));
sizeAllText = MergeLines(&allText, sizeAllText, &source, lines[a].GetBuffer(0));
The calls to GetBuffer() are not necessary. Remove them.
c121hains wrote:
int CFindInWindowDlg::MergeLines(char** allText, int sizeSource, char** source, char* aLine)
Change the last parameter to const char * instead.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
When I attach the CStrings together (newString = string[0] + string[1] + string[2] ..), the result does not correctly append the strings. My strings contain '\n' and '\r' characters and it seems that when I use the newString += string, only the first result is copied.
Here is an example:
string1 = "this is a string \r\n";
string2 = "this is an appended string \r\n";
string1 += string2;
string1 still equals "this is a string \r\n" and doesn't append string2!!
I need those '\r' and '\n' characters. By putting them all into a char string, it fixes this problem.
|
|
|
|
|
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:
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 a CString 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 use GetBuffer ; that's not the problem.
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
A classic case of "MFC must be broken because my code works just fine."
Wrong.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Try this:
CString strAll = lines[0] + lines[1] ...
|
|
|
|
|
I tried that but it didn't work.
Here is an example of what happens:
string1 = "this is a string \r\n";
string2 = "this is an appended string \r\n";
string1 += string2;
string1 still equals "this is a string \r\n" and doesn't append string2!!
I need those '\r' and '\n' characters. By putting them all into a char string, it fixes this problem.
|
|
|
|
|
|
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/[^]
|
|
|
|
|
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?
|
|
|
|
|
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
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.
|
|
|
|
|
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)
{
int nTotalLength = 1;
for (int i = 0; i < nLines; i++)
nTotalLength += lines[i].GetLength();
result.Empty();
result.GetBuffer(nTotalLength);
result.ReleaseBuffer();
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?
|
|
|
|
|
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/[^]
|
|
|
|
|
Hello,
if I use AfxBeginThread() function to start a thread, how do I check if it's still running or has already exited?
|
|
|
|
|
Normally you shouldn't care. What exactly is it that you are attempting to do?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I have a thread that plays a sequence of images. In the CDocument I handle controls to pause/play using events and boolean variables. I chose start the thread inside play() function. The way I handle pause() I don't care if the thread is there or not. However if the user presses play again, I don't want to start another thread, I want to continue the previous one.
I have the option of making a thread that runs for the life of the CDocument...but wouldn't that be wasteful when it sits there and waits on pause forever after a single play.
|
|
|
|
|
Also I should clarify currently the thread goes through the sequence of images and quits. So next time inside play() I need to know whether to resume the current thread or start a new one.
|
|
|
|
|
See here for details on how to properly pause and stop threads.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Yes, that's what I use. Taken from the site:
<br />
while(running)<br />
{ <br />
if(paused)<br />
switch(::WaitForSingleObject(event, time))<br />
{<br />
case WAIT_OBJECT_0:<br />
break;<br />
case WAIT_TIMEOUT:<br />
continue;<br />
}<br />
} <br />
except instead of while(running), I have a for loop from 0 to numImages.
|
|
|
|
|
Budric B. wrote:
but wouldn't that be wasteful when it sits there and waits on pause forever after a single play.
Not neccessarily. Or at least I wouldn't think so if you're using WaitForSingleObject() to do the "waiting" and not a busy wait while loop.
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
|
|
|
|
|
Jim Crafton wrote:
...if you're using WaitForSingleObject() to do the "waiting" and not a busy wait while loop.
Correct. WaitForSingleObject() uses no CPU time while waiting for the object state to become signaled or the time-out interval to elapse.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|