variable argument print function

来源:互联网 发布:SQL默认值为0 编辑:程序博客网 时间:2024/05/19 10:41

//variable argument print function ,replace the fputchar() with your log function.


#include "stdafx.h"
#include <stddef.h>

#include<stdio.h>
#include <stdarg.h>

#define MAX_LEN  100
void vspf(char *fmt, ...)
{
char err[] = "Err:str len is larger than MAX_LEN !!!\n";
char str[MAX_LEN + 1] = {0};
char *ptr = str;
va_list argptr;
int len;

va_start(argptr, fmt);
len = vsnprintf(str, MAX_LEN, fmt, argptr);
va_end(argptr);

if(len < 0)
ptr = err;
while(*ptr)
fputchar(*ptr++);
}

int _tmain(int argc, _TCHAR* argv[])
{
int iVar = 30;
float fVar = 90.0;
char str[4] = "abc";


vspf("The iVar is :%d\n The fVar is %f\n The str is: %s\n", iVar, fVar, str);


getchar();

return 0;
}

0 0