网络服务器软件开发/中间件开发,关注ACE/ICE/boost

C++博客 首页 新随笔 联系 聚合 管理
  152 Posts :: 3 Stories :: 172 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 阅读(2303) 评论(2)  编辑 收藏 引用 所属分类: C++基础

Feedback

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

# re: VC6中没有vsnprintf函数 2008-12-16 17:39 雲飛揚
加一个下划线既可.

n = _vsnprintf (p, size, fmt, ap);  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理