your own printf

Post new topic   Reply to topic

View previous topic View next topic Go down

your own printf

Post  Admin on Sat Sep 06, 2008 5:05 am

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 5:52 pm

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

View previous topic View next topic Back to top


Post new topic   Reply to topic
Permissions of this forum:
You cannot reply to topics in this forum