S
some
Hi All,
I am trying to write a function similar to scanf which uses scanf to do
most of the work. It is working correctly for char,int but does not
work for double. I read the FAQ's 11.3,15.2,15.10 from
http://www.eskimo.com/~scs/C-faq/ but that could not help me. I would
appreciate any help.
Please excuse if my question is in a inccorrect group or very silly of
me to ask such a thing.
Thanks a lot.
I wrote something similar to this
I know the code is elementary and has lot of bugs.
////////////////////////////////////////////////
#include<stdio.h>
#include<stdarg.h>
#define MAX 25
void scanfunction(char *fmt,...);
int main(int argc,char *argv[]) {
int num;
char chr;
double floatnum;
char str[MAX];
scanfunction("%d %c %s %f ",&num,&chr,str,&floatnum);
return 0;
}
void scanfunction (char *fmt,...) {
va_list arg;
char *str,*s;
int *num;
char *chr;
double *floatnum;
va_start(arg,fmt);
for(s=fmt;*s;s++) {
if(*s != '%')
continue;
switch(*++s) {
case 'd' :
num = va_arg(arg,int*);
fflush(stdin);
printf("enter a num \n ");
scanf("%d",num);
printf("we read %d \n ",*num);
break;
case 'c' :
chr = va_arg(arg,char*);
fflush(stdin);
printf("enter a char \n ");
scanf("%c",chr);
printf("we read %c \n ",*chr);
break;
case 'f' :
floatnum = va_arg(arg,double*);
fflush(stdin);
printf("enter a float \n ");
scanf("%f",floatnum);
printf("we read %f \n ",*floatnum);
break;
case 's' :
str = va_arg(arg,char*);
fflush(stdin);
printf("enter a string \n ");
scanf("%s",str);
printf("we read %s \n ",str);
break;
default :
printf("incorrect format to be inputted \n");
break;
}
}
va_end(arg);
}
///////////////////////////////
Thanks a lot.
I am trying to write a function similar to scanf which uses scanf to do
most of the work. It is working correctly for char,int but does not
work for double. I read the FAQ's 11.3,15.2,15.10 from
http://www.eskimo.com/~scs/C-faq/ but that could not help me. I would
appreciate any help.
Please excuse if my question is in a inccorrect group or very silly of
me to ask such a thing.
Thanks a lot.
I wrote something similar to this
I know the code is elementary and has lot of bugs.
////////////////////////////////////////////////
#include<stdio.h>
#include<stdarg.h>
#define MAX 25
void scanfunction(char *fmt,...);
int main(int argc,char *argv[]) {
int num;
char chr;
double floatnum;
char str[MAX];
scanfunction("%d %c %s %f ",&num,&chr,str,&floatnum);
return 0;
}
void scanfunction (char *fmt,...) {
va_list arg;
char *str,*s;
int *num;
char *chr;
double *floatnum;
va_start(arg,fmt);
for(s=fmt;*s;s++) {
if(*s != '%')
continue;
switch(*++s) {
case 'd' :
num = va_arg(arg,int*);
fflush(stdin);
printf("enter a num \n ");
scanf("%d",num);
printf("we read %d \n ",*num);
break;
case 'c' :
chr = va_arg(arg,char*);
fflush(stdin);
printf("enter a char \n ");
scanf("%c",chr);
printf("we read %c \n ",*chr);
break;
case 'f' :
floatnum = va_arg(arg,double*);
fflush(stdin);
printf("enter a float \n ");
scanf("%f",floatnum);
printf("we read %f \n ",*floatnum);
break;
case 's' :
str = va_arg(arg,char*);
fflush(stdin);
printf("enter a string \n ");
scanf("%s",str);
printf("we read %s \n ",str);
break;
default :
printf("incorrect format to be inputted \n");
break;
}
}
va_end(arg);
}
///////////////////////////////
Thanks a lot.