strprintf
strprintf is the C++ version of sprintf. It returns a string instead of requiring a char array.
typedef std::basic_string<TCHAR> tstring;
tstring strprintf(LPCTSTR format, ...)
{
va_list argList;
va_start(argList, format);
TCHAR text[1024] = _T("");
_vsntprintf(text, 1024, format, argList);
va_end(argList);
return text;
}
Arguments
- format
- The format of the string. See this page for the possible values.
- ... (variable arguments)
- The variables to be included in the string, basing on the format parameter.
Example
tstring string = _T("hello");
TCHAR *world = _T("world");
int number = 2;
tstring result = strprintf(_T("%s %s %d"), hello.c_str(), world, number);
Comments
kkez wrote:
It can. It actually returns a string (tstring) class, which accepts the local pointer to a string. Try it, it works.
(26.09.2007, 23:17)
Paul wrote:
This can't work, can it? You are returning a pointer to a stack-based variable, and the variable will go away when you return!
The way I do this is to malloc (or new) a buffer and copy the formatted string into it. Caller must then free (or delete)
(25.09.2007, 14:46)
Script by Alex
This page was last modified
34 months, 3 weeks, 2 days and 9 hours ago