像下面的好用工具函数就无法使用:
string format_string(const char *fmt, ...)
{
/* Guess we need no more than 512 bytes. */
int n, size = 512;
char *p;
va_list ap;
if ((p = (char*)malloc (size)) == NULL)
return "";
while (1)
{
/* Try to print in the allocated space. */
va_start(ap, fmt);
n = vsnprintf (p, size, fmt, ap);
va_end(ap);
/* If that worked, return the string. */
if (n > -1 && n < size)
{
string str(p);
if(p)
free(p);
return str;
}
/* Else try again with more space. */
if (n > -1) /* glibc 2.1 */
size = n+1; /* precisely what is needed */
else /* glibc 2.0 */
size *= 2; /* twice the old size */
if ((p = (char*)realloc (p, size)) == NULL)
{
if(p)
free(p);
return "";
}
}
}