source code of scanf

gyanu's Avatar, Join Date: Jan 2008
Light Poster
I want to create a new function myscanf (char *format,...) which does same as scanf() without using any library function other than getchar().
Pls soomeone get it done for meeeeee.plssssssssssss
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Code:
char c;
do 
{
    c=getchar();
    putchar (c);
} while (c != '\n');
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
Quote:
Originally Posted by shabbir
Code:
char c;
do 
{
    c=getchar();
    putchar (c);
} while (c != '\n');

You are still using library function getchar() and putchar().

He is asking if i am correct.


Code:
#include "stddef.h"
#include "stdarg.h"
#include "stdlib.h"

extern FILE *Stderr;


int  MyScanf(char *s, const char *fmt, ...)
{
	char *s0;
	va_list ap;
	long L, *Lp;
	int i, *ip, rc = 0;
	va_start(ap, fmt);
	for( ; ; ) {
		for(;;) {
			switch(i = *(unsigned char *)fmt++) {
				case 0:
					goto done;
				case '%':
					break;
				default:
					if (i <= ' ') {
						while(*s <= ' ')
							if (!*s++)
								return rc;
						}
					else if (*s++ != i)
						return rc;
					continue;
				}
			break;
			}
		switch(*fmt++) {
			case 'l':
				if (*fmt != 'd')
					bad(fmt);
				fmt++;
				Lp = va_arg(ap, long*);
				L = strtol(s0 = s, &s, 10);
				if (s > s0) {
					rc++;
					*Lp = L;
					continue;
					}
				return rc;
			case 'd':
				ip = va_arg(ap, int*);
				L = strtol(s0 = s, &s, 10);
				if (s > s0) {
					rc++;
					*ip = (int)L;
					continue;
					}
				return rc;
			default:
				bad(fmt);
			}
		}
 done:
	return rc;
	}
	}
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
Oh!! At the last of code add one function

va_end ( ap );
0
gyanu's Avatar, Join Date: Jan 2008
Light Poster
Quote:
Originally Posted by gyanu
I want to create a new function myscanf (char *format,...) which does same as scanf() without using any library function other than getchar().
Pls soomeone get it done for meeeeee.plssssssssssss



Shabbir bhai, hi
U have helped me really.......... But my problem is not about supporting integer but rather %s,for string datatype char buf[100]
%x, for hexadecimal
%c for characters
please help me little bit more
0
gyanu's Avatar, Join Date: Jan 2008
Light Poster
Quote:
Originally Posted by asadullah.ansari
You are still using library function getchar() and putchar().

He is asking if i am correct.


Code:
#include "stddef.h"
#include "stdarg.h"
#include "stdlib.h"

extern FILE *Stderr;


int  MyScanf(char *s, const char *fmt, ...)
{
	char *s0;
	va_list ap;
	long L, *Lp;
	int i, *ip, rc = 0;
	va_start(ap, fmt);
	for( ; ; ) {
		for(;;) {
			switch(i = *(unsigned char *)fmt++) {
				case 0:
					goto done;
				case '%':
					break;
				default:
					if (i <= ' ') {
						while(*s <= ' ')
							if (!*s++)
								return rc;
						}
					else if (*s++ != i)
						return rc;
					continue;
				}
			break;
			}
		switch(*fmt++) {
			case 'l':
				if (*fmt != 'd')
					bad(fmt);
				fmt++;
				Lp = va_arg(ap, long*);
				L = strtol(s0 = s, &s, 10);
				if (s > s0) {
					rc++;
					*Lp = L;
					continue;
					}
				return rc;
			case 'd':
				ip = va_arg(ap, int*);
				L = strtol(s0 = s, &s, 10);
				if (s > s0) {
					rc++;
					*ip = (int)L;
					continue;
					}
				return rc;
			default:
				bad(fmt);
			}
		}
 done:
	return rc;
	}
	}
ansari bhai
Thanks a lot .U r great but my problems are little more tough.I should not use myscanf(char *s,char *fmt,...)
Also it should support %c,%x and %s for strings too
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by gyanu
Shabbir bhai, hi
U have helped me really.......... But my problem is not about supporting integer but rather %s,for string datatype char buf[100]
%x, for hexadecimal
%c for characters
please help me little bit more
I could not get you.
0
oogabooga's Avatar
Ambitious contributor
Quote:
Originally Posted by gyanu
but my problems are little more tough.I should not use myscanf(char *s,char *fmt,...) Also it should support %c,%x and %s for strings too
Could we please see some of your code?
I've seen this on another forum, and I've yet to see any code from you.
People are not just going to do it for you (hopefully).
If you want to cheat, look up some open source code for scanf.
0
gyanu's Avatar, Join Date: Jan 2008
Light Poster
Quote:
Originally Posted by shabbir
I could not get you.
I have to create and implement a function myscanf(char *format,...) wilthout using any library functions other than getchar().
My program would support to undersand---------myscanf("%d%s%x",a,b[],c);
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
The string parsing and variable number of arguments in a function is all together a diiferent topic and you can check out - OutputDebugString with variable number of arguments for that but I guess your initial requirement is fully satisfied with the code snippet I provided.