-
sprintf
Hello I am just wondering how do I convert my strings to the "sprintf" without any errors or possible losses of data.
Code:
TCHAR chText[150];
TCHAR chText2[150];
TCHAR chLine[300];
chLine = sprintf(chText, " - ", chText2);
The error note if it helps: "error C2664: 'sprintf' : cannot convert parameter 2 from 'TCHAR' to 'const char *"
Thx
-
If you're using potentially wide character strings under Windows you're advised to use wsprintf.
-
Well the various manual pages on MSDN tell you how to use TCHAR effectively so that functions which use narrow and wide chars will be used correctly depending on your project settings.
-
You use sprintf incorrectly. If you want to copy a string, you should use %s.
-
Try this code:
Code:
TCHAR chText[150];
TCHAR chText2[150];
TCHAR chLine[300];
strcpy(chLine, chText);
strcat(chLine, " - ");
strcat(chLine, chText2);
-
I recomend using _snprintf() if using a MS IDE (compiler)
Code:
sprintf(chLine,"%s -- %s",chText,chText2);
But there is no check that the text will fit into the chLine array. If it does not then BANG/SPLAT!
Code:
_snprintf(chLine,299,"%s -- %s",chText,chText2);// you can set the max number of characters added to the buffer to avoid buffer over-run
-
Actually you really should use a pointer if you have variable text sizes, like this:
Code:
TCHAR chText[150];
TCHAR chText2[150];
TCHAR *pchLine =NULL;
pchLine = (TCHAR*)malloc(strlen(chText) + strlen(chText2) + 4);
strcpy(pchLine, chText);
strcat(pchLine, " - ");
strcat(pchLine, chText2);
// Do shtufff...
free(pchLine); // Be sure to clean up.
-
>Actually you really should use ...
Actually, shouldn't you be using functions other than strcpy, strcat if you are using TCHAR? None of which appear to have been mentioned yet -- except where Salem recommended a tour of the MSDN?
-
Thank you for helping now the app. works properly