your own printf

Go down

your own printf

Post  Admin on Fri Sep 05, 2008 11:05 pm

How can I arrange to have output go two places at once,
e.g. to the screen and to a file?write your own printf function to do this?

Admin
Admin

Posts : 39
Join date : 2008-08-03

View user profile http://computerclub09mnnit.forumotion.net

Back to top Go down

@admins--COPIED SOLUTION

Post  prani on Mon Sep 08, 2008 11:52 am

this has been copied as it is from k&R
#include<stdio.h>
#include<stdarg.h>

int meraPrintf(char *str,...)
{
va_list var;
char *p,*s;
int num;
double d;
va_start(var,str);
FILE *cout=fopen("file.txt","w");
for(p=str;*p;p++)
{
if(*p!='%')
{
putchar(*p);
fputc(*p,cout);

}
switch(*++p)
{
case 'd':
num=va_arg(var,int);
printf("%d",num);
fprintf(cout,"%d",num);
break;
case 'f':
d=va_arg(var,double);
printf("%f",d);
fprintf(cout,"%f",d);
break;
case 's':
for(s=va_arg(var,char *);*s;s++)
{
putchar(*s);
fputc(*s,cout);

}
break;
default:
putchar(*p);
fputc(*p,cout);
break;
}
}
va_end(var);
}
int main()
{

meraPrintf("ballu");
meraPrintf("%d\n",1000);
getchar();
}
source:K&R page 155

prani

Posts : 15
Join date : 2008-08-12

View user profile

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum