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 |
|
Code:
char c;
do
{
c=getchar();
putchar (c);
} while (c != '\n');
|
|
TechCake
|
|
| 31Jan2008,11:34 | #3 |
|
Quote:
Originally Posted by shabbir 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 |
|
Oh!! At the last of code add one function
va_end ( ap ); |
|
Light Poster
|
|
| 1Feb2008,10:16 | #5 |
|
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 |
|
Light Poster
|
|
| 1Feb2008,10:20 | #6 |
|
Quote:
Originally Posted by asadullah.ansari 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 |
|
Quote:
Originally Posted by gyanu |
|
Ambitious contributor
|
|
| 1Feb2008,21:44 | #8 |
|
Quote:
Originally Posted by gyanu 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 |
|
Quote:
Originally Posted by shabbir My program would support to undersand---------myscanf("%d%s%x",a,b[],c); |
|
Go4Expert Founder
|
![]() |
| 3Feb2008,11:29 | #10 |
|
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.
|

