source code of scanf

Light Poster
31Jan2008,05:39   #1
gyanu's Avatar
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
Go4Expert Founder
31Jan2008,08:08   #2
shabbir's Avatar
Code:
char c;
do 
{
    c=getchar();
    putchar (c);
} while (c != '\n');
TechCake
31Jan2008,11:34   #3
asadullah.ansari's Avatar
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;
	}
	}
TechCake
31Jan2008,11:38   #4
asadullah.ansari's Avatar
Oh!! At the last of code add one function

va_end ( ap );
Light Poster
1Feb2008,10:16   #5
gyanu's Avatar
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
Light Poster
1Feb2008,10:20   #6
gyanu's Avatar
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
Go4Expert Founder
1Feb2008,11:31   #7
shabbir's Avatar
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.
Ambitious contributor
1Feb2008,21:44   #8
oogabooga's Avatar
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.
Light Poster
2Feb2008,05:55   #9
gyanu's Avatar
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);
Go4Expert Founder
3Feb2008,11:29   #10
shabbir's Avatar
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.