PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Text handling

summer_goth · 20 Nov 2006, 20:26 UTC
I've just got a question about text handling.

With PocketFrog I used to read text from a text file using _fgetts. Now I use readLine in GledPlay.

I am just not sure how to copy various parts of the text that I have read into another variable. Wow I hope I make this clear enough.

Code:

TCHAR buf[119];
TCHAR temp[21];

textFile.readLine(buf, 0);
for (i=0;i<20;i++)
{
temp[i] = buf[i+86];
}
temp[21] = '\0';


When I copy some of the characters over to the temp it seems that the whole of buf from buf[86] is copied and not just one character at a time.

Then when I null terminate ('\0') temp it seems that the characters are still there. For instance when I use the font class to render the text in temp it displays the characters that I copied plus the rest of the characters that were in buf.

Is there a different proper way to copy some text from the buffer that was read? Sorry I am still rather new to this and I hope this question makes sense.

Thanks guys.
Hi summer_goth,

I think your problem is in this line:

Code:
temp[21] = '\0';


You've declared:

Code:
TCHAR temp[21];


That means that you have 21 slots to fill, from temp[0] to temp[20].
You are writing a 0 in a memory slot that doesn't belong to you (temp[21]). Strange things happen when you do that...

try changing the temp declaration to this:

Code:
TCHAR temp[22];


In fact, this may work faster:

Code:
TCHAR buf[119];
TCHAR temp[22];

textFile.readLine(buf, 0);
memcpy(temp, buf+86, sizeof(TCHAR)*21);
temp[21]=0;


I hope this solves the problem. If not, please let me know and we'll try harder :D

Best regards,
Sergio.
summer_goth · 21 Nov 2006, 09:26 UTC
Thank you very much Sergio. It works perfectly now. :D

Have a great time.