网络服务器软件开发

C++博客 首页 新随笔 联系 聚合 管理
  87 Posts :: 3 Stories :: 52 Comments :: 0 Trackbacks

像下面的好用工具函数就无法使用:
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 "";
  }
 }
}  

posted on 2008-10-07 10:17 true 阅读(132) 评论(1)  编辑 收藏 引用 所属分类: C++基础

Feedback

# re: VC6中没有vsnprintf函数 2008-10-07 16:26 李现民
的确没有,这也的确是个问题  回复  更多评论
  


标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航: