构造一个类似printf的函数,输出到字符串

来源:百度知道 编辑:UC知道 时间:2024/05/16 18:23:49
printf可以可变参数输入,类似
printf("my name is %s","Nicky");
如何构造一个类似printf的函数使其结果输出到一个字符串?

#include <iostream.h>

void MyPrint(const char *s,const char *sIn)
{
/******************************************
input : s: format sIn,nubers,list,strings,and soon which
separated by ',',
Examples:
s = "%d is %s", sIn = "3,my favorite number;"
output : 3 is my favorite number;
but only one single formation letter is permited
exceptions: %lf cannot be known by this simple implement
this is only one simulation for the print

*****************************************/
int i = 0;
int nIndex = 0;
int nResultIndex = 0;
char sResult[50];
while(s[i]!='\0')
{
if(s[i]=='%')// no detect the type for simpfied
{
i = i+2;
while(sIn[nIndex]!='\0'&&sIn[nIndex]!=',')
{
sResult[nResultIndex++] = sIn[nIndex++];
}
nIndex++;
}
sResult