I'm having a hard time manipulating these strings in eVC. :( How would you cut a portion of a string? What function should I use? For example, I have the string "thingamajig" and would like to cut the first 5 letters and print it on the screen. How do I do that and which function should I use?
Thanks in advance.
String is not a builtin data type. What kind of strings are you talking about?
char* ?
std::string?
MFC CString?
your own string class?
In C++, I use std::string and its associated functions.
Let me rephrase what he just asked:
Is there a ppc version for the code below
TCHAR ls_Temp[10];
memmove(ls_Temp, fu_GameData[SLOT1].Name, 10);
It works fine on PC, won't compile for PPC.
Thanks
I just tried
- Code:
TCHAR ls_Temp[10];
memmove(ls_Temp, _T("hello everyone"), 10*sizeof(TCHAR));
and it compiled just fine!
_tcscpy() should also do fine if string copying is your purpose in the example you gave.
egarayblas wrote:
For example, I have the string "thingamajig" and would like to cut the first 5 letters and print it on the screen. How do I do that and which function should I use?
One way would just be to memcpy() or memmove() to copy the first 5 TCHARs (remember, on PPC a TCHAR is 2 bytes; you have to take that into account) into another buffer and print that... or _tcscpy() into another buffer and set the 6th tchar to null. Lots of possibilities.
PejoSoftware wrote:
I just tried
- Code:
TCHAR ls_Temp[10];
memmove(ls_Temp, _T("hello everyone"), 10*sizeof(TCHAR));
and it compiled just fine!
I tried the code on top and it returned this:
error C2065: 'Lls_Temp' : undeclared identifier
Where did I go wrong?
That's exceedingly weird. Try this little program:
- Code:
#include <windows.h>
#include <tchar.h>
#include <stdlib.h>
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
TCHAR ls_Temp[10];
memmove(ls_Temp, _T("hello everyone"), 10*sizeof(TCHAR));
ls_Temp[10] = 0;
MessageBox(NULL, ls_Temp, L"see, it works!", MB_OK);
return 234932;
}
And it doesn't compile? Because it should. Are you using eVC 4? That might be the problem.
I would use _tcscpy as it is tailored for generic text copying. It will get the character count right on Pocket PC (which has wide characters.) Don't use memcpy, that is a low level routine.
But if you are using C++, I'd strongly recommend using std::basic_string<_TCHAR> which will work like std::string on PC and std::wstring on Pocket PC. (Actually, it *will* be std::string on PC and std::wstring on Pocket PC. Typedefs don't create types, just aliases.)
Then, you can just use the standard library's routines for manipulating standard strings. And you don't have to worry so much about managing their memory, or fixed-sized issues, or shrinking/growing them.
And I wouldn't worry about performance. Games typically don't do much string-crunching, and even still, implementations of string are typically fairly optimized. In fact, they typically store small strings within the object themselves, and only go to the heap if the string really grows big.
(In fact, it uses the free store which is distinct from the heap, but now I'm nitpicking.)
And going slightly more off topic:
You could use rope if you need faster concatenation and substring operations with long strings.
STL rules!
I've seen my mistake, its working now. Thanks for all your help!
Whenever I need to "cut" strings, I just use scissor code.
