跨平台vsnpritf和va_list

void FormatOutput(const char* lpszFormat, ...)
{
 va_list argList;
 va_start(argList, lpszFormat);

 int flat = -1;
 int size = 100;
 char* buf = NULL;

 while(flat < 0)
 {
  char* buf = NULL;

  while (flat<0)
  {
   if (buf) {
    delete[] buf;
    buf = NULL;
   }
#ifdef _WIN32
   buf = new char[size + 1];
   if (buf == NULL) {
    va_end(argList);
    return ;
   }
   //如果源串长度超过目的串,则返回-1;没超过的话,拷贝给定长度的字符,不包括'\0'
   flat = _vsnprintf (buf, size, lpszFormat, argList);
   if (flat<0) {
    size += 100;
   }
#else  //在linux上返回其所需长度
   va_list argDest;
   va_copy(argDest, argList);
   size = vsnprintf(NULL, 0, lpszFormat, argDest);
   buf = new char[size + 1];
   if (buf == NULL) {
    va_end(argList);
    return ;
   }

   int tf = vsnprintf(buf, size+1, lpszFormat, argList);
   printf(buf);
   printf("\n");
   if (tf>size) {
    flat = -1;
    size = tf + 1;
   }
   else {
    flat = tf;
   }
#endif
  }
  buf[flat] = 0;

  printf(buf);
  printf("\n");
 }
 va_end(argList);
}

你可能感兴趣的:(跨平台vsnpritf和va_list)