This topic is 4220 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

how can you add 2 srtrings of unicode TCHAR together to print out in a font obejct of directX, for example TCHAR *fps,testv[20]; int q=8; fps=L"asdasd"; swprintf(testv,L"%s",q); fps=fps + testv; g_font->DrawText(NULL, testv, -1, &test, DT_LEFT|DT_TOP, 0xffffffff);

Share on other sites
Are you using C or C++? Assuming you use C, to add wide strings together you use wcscat (strcat for char strings). So your code should look like this:
TCHAR *fps,testv[20];int q = 8;fps=L"asdasd";swprintf(testv,L"%s",q);wcscat(fps,testv);// Shouldn't you print out fps instead of testv?g_font->DrawText(NULL, fps, -1, &test,DT_LEFT|DT_TOP, 0xffffffff);

EDIT: You should remember TCHARs isn't wide string, they can be chars or wchar_ts depending on defines. Your code will fail if TCHAR is char.

Share on other sites
Quote:
 Original post by CTarAre you using C or C++? Assuming you use C, to add wide strings together you use wcscat (strcat for char strings). So your code should look like this:TCHAR *fps,testv[20];int q = 8;fps=L"asdasd";swprintf(testv,L"%s",q);wcscat(fps,testv);// Shouldn't you print out fps instead of testv?g_font->DrawText(NULL, fps, -1, &test,DT_LEFT|DT_TOP, 0xffffffff); EDIT: You should remember TCHARs isn't wide string, they can be chars or wchar_ts depending on defines. Your code will fail if TCHAR is char.

Using c++ but I just want the simplest way to do it.
I am using unicode so TCHAR defaults to cater for this I believe.

Share on other sites
can't add 2 TCHAR* together but can do arrays.

TCHAR *fps,testv[20],*fp,testvv[20];

fp=L"iii";
fps=L"asdasd";

swprintf(testvv,L"%s",fps );

g_font->DrawText(NULL, testvv, -1, &test,
DT_LEFT|DT_TOP, 0xffffffff);

swprintf(testv,L"%d",offsetX);
wcscat(testv,testvv);
wcscat(fp,fps);//access violation

Share on other sites
Because the memory where the string would be concatenated (not "added") doesn't necessarily belong to you. An array is an actual reserved chunk of memory. A pointer merely says "there's something over there". All wcscat() is doing, essentially, is copying data blindly from one location (starting at beginning of second string, for the length of second string) to another (end of first string).

Count yourself lucky it crashed: it could instead overwrite some other variable, causing something much harder to figure out.

In C++, you should be using the std::string to represent ordinary strings. As it happens, there is also std::wstring for unicode strings, with which you can just use the operator+, nice and clean. You can set up a define similar to how TCHAR works, in order to keep the ability to switch UNICODE on and off:

#ifdef UNICODEtypedef std::wstring TSTRING;#elsetypedef std::string TSTRING;#endifTSTRING fp(L"iii");TSTRING fps(L"asdasd");TSTRING fpfps = fp + fps;