Implementing scanf with getchar

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 3
Reputation: gyanu is an unknown quantity at this point 
Solved Threads: 0
gyanu gyanu is offline Offline
Newbie Poster

Re: Source code for printf, scanf, cin, cout?

 
0
  #1
Feb 15th, 2008
//I tried to fix the errors but I failed.
//pls do it for me.......


  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a;
  6. void scanchar(char**);
  7. int scans(char**,char*,int,int);
  8. int scani(char**,int,int,int,int,int,int);
  9. int myscanf(char *);
  10. int smyscanf(char*,char*);
  11. clrscr();
  12. printf("enter");
  13. myscanf("%d",&a);
  14. printf("%d",a);
  15. getch();
  16. }
  17. static void scanchar(char **str)
  18. {
  19. extern int getchar();
  20. if (str) {
  21. **str = getchar();
  22. ++(*str);
  23. }
  24. else (void)getchar();
  25. }
  26. #define PAD_RIGHT 1
  27. #define PAD_ZERO 2
  28. static int prints(char **in, const char *string, int width, int pad)
  29. {
  30. register int sc = 0, padchar = ' ';
  31. if (width > 0) {
  32. register int len = 0;
  33. register const char *ptr;
  34. for (ptr = string; *ptr; ++ptr) ++len;
  35. if (len >= width) width = 0;
  36. else width -= len;
  37. if (pad & PAD_ZERO) padchar = '0';
  38. }
  39. if (!(pad & PAD_RIGHT)) {
  40. for ( ; width > 0; --width) {
  41. scanchar (in, padchar);
  42. ++sc;
  43. }
  44. }
  45. for ( ; *string ; ++string) {
  46. scanchar (in, *string);
  47. ++sc;
  48. }
  49. for ( ; width > 0; --width) {
  50. scanchar (in, padchar);
  51. ++sc;
  52. }
  53. return sc;
  54. }
  55. /* the following should be enough for 32 bit int */
  56. #define SCAN_BUF_LEN 12
  57. static int scani(char **in, int i, int b, int sg, int width, int pad, int letbase)
  58. {
  59. char scan_buf[SCAN_BUF_LEN];
  60. register char *s;
  61. register int t, neg = 0, sc = 0;
  62. register unsigned int u = i;
  63. if (i == 0) {
  64. scan_buf[0] = '0';
  65. scan_buf[1] = '\0';
  66. return scans (out, print_buf, width, pad);
  67. }
  68. if (sg && b == 10 && i < 0) {
  69. neg = 1;
  70. u = -i;
  71. }
  72. s = scan_buf + SCAN_BUF_LEN-1;
  73. *s = '\0';
  74. while (u) {
  75. t = u % b;
  76. if( t >= 10 )
  77. t += letbase - '0' - 10;
  78. *--s = t + '0';
  79. u /= b;
  80. }
  81. if (neg) {
  82. if( width && (pad & PAD_ZERO) ) {
  83. scanchar (in,'-');
  84. ++sc;
  85. --width;
  86. }
  87. else {
  88. *--s = '-';
  89. }
  90. }
  91. return sc + scans (in, s, width, pad);
  92. }
  93. static int scan(char **in, int *varg)
  94. {
  95. register int width, pad;
  96. register int sc = 0;
  97. register char *format = (char *)(*varg++);
  98. char scr[2];
  99. for (; *format != 0; ++format) {
  100. if (*format == '%') {
  101. ++format;
  102. width = pad = 0;
  103. if (*format == '\0') break;
  104. if (*format == '%') goto in;
  105. if (*format == '-') {
  106. ++format;
  107. pad = PAD_RIGHT;
  108. }
  109. while (*format == '0') {
  110. ++format;
  111. pad |= PAD_ZERO;
  112. }
  113. for ( ; *format >= '0' && *format <= '9'; ++format) {
  114. width *= 10;
  115. width += *format - '0';
  116. }
  117. if( *format == 's' ) {
  118. register char *s = *((char **)varg++);
  119. sc += scans (in, s?s:"(null)", width, pad);
  120. continue;
  121. }
  122. if( *format == 'd' ) {
  123. sc += scani (in, *varg++, 10, 1, width, pad, 'a');
  124. continue;
  125. }
  126. if( *format == 'x' ) {
  127. sc += scani (in, *varg++, 16, 0, width, pad, 'a');
  128. continue;
  129. }
  130. if( *format == 'X' ) {
  131. sc += scani (in, *varg++, 16, 0, width, pad, 'A');
  132. continue;
  133. }
  134. if( *format == 'u' ) {
  135. sc += scani (in, *varg++, 10, 0, width, pad, 'a');
  136. continue;
  137. }
  138. if( *format == 'c' ) {
  139. /* char are converted to int then pushed on the stack */
  140. scr[0] = *varg++;
  141. scr[1] = '\0';
  142. sc += scans (in, scr, width, pad);
  143. continue;
  144. }
  145. }
  146. else {
  147. in:
  148. scanchar (in, *format);
  149. ++sc;
  150. }
  151. }
  152. if (in) **in = '\0';
  153. return sc;
  154. }
  155. /* assuming sizeof(void *) == sizeof(int) */
  156. int myscanf(const char *format, ...)
  157. {
  158. register int *varg = (int *)(&format);
  159. return scan(0, varg);
  160. }
  161. int smyscanf(char *out, const char *format, ...)
  162. {
  163. register int *varg = (int *)(&format);
  164. return scan(&out, varg);
  165. }
Last edited by Ancient Dragon; Feb 15th, 2008 at 10:09 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: Source code for printf, scanf, cin, cout?

 
0
  #2
Feb 15th, 2008
Use code tags. Mention specific problems and the line no.s. The size of the code is a bit too lengthy. Proper formatting and specific questions would take you a long way ahead in getting help.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Source code for printf, scanf, cin, cout?

 
0
  #3
Feb 15th, 2008
what are a few of the error messages you got ? What compiler are you using ? My guess is that you did not write the code you posted because its too advanced for someone that asked the question you asked.

The code appears to have been written with Turbo C compiler ( clrscr() is only supported by Turbo C) so you will have to rewrite parts of that code that contains Borland-specific functions.
Last edited by Ancient Dragon; Feb 15th, 2008 at 10:22 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Source code for printf, scanf, cin, cout?

 
0
  #4
Feb 15th, 2008
That looks like a healthy mix up of scanf and printf. Why are you trying to do padding in scanf? That code is actually quite awful, so you probably want to redesign and rewrite it. I get the impression that this isn't your code, so I'll give you a better example to start with:
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4.  
  5. int myscanf ( const char *fmt, ... );
  6. static int trim_leading ( void );
  7. static int myscanf_internal ( const char *fmt, va_list args );
  8.  
  9. int main ( void )
  10. {
  11. char s[100];
  12. int a;
  13.  
  14. myscanf ( "%s%d", s, &a );
  15. printf ( "%s\n%d\n", s, a );
  16.  
  17. return 0;
  18. }
  19.  
  20. int myscanf ( const char *fmt, ... )
  21. {
  22. va_list args;
  23. int rv;
  24.  
  25. va_start ( args, fmt );
  26. rv = myscanf_internal ( fmt, args );
  27. va_end ( args );
  28.  
  29. return rv;
  30. }
  31.  
  32. static int trim_leading ( void )
  33. {
  34. int ch;
  35.  
  36. do {
  37. if ( ( ch = getchar() ) == EOF )
  38. break;
  39. } while ( isspace ( ch ) );
  40.  
  41. ungetc ( ch, stdin );
  42.  
  43. return ch;
  44. }
  45.  
  46. static int myscanf_internal ( const char *fmt, va_list args )
  47. {
  48. int converted = 0;
  49.  
  50. while ( *fmt != '\0' ) {
  51. if ( *fmt++ == '%' ) {
  52. switch ( *fmt ) {
  53. case 's':
  54. {
  55. char *dst = va_arg ( args, char* );
  56. int ch;
  57.  
  58. trim_leading();
  59.  
  60. while ( ( ch = getchar() ) != EOF && !isspace ( ch ) )
  61. *dst++ = (char)ch;
  62. *dst = '\0';
  63.  
  64. ++converted;
  65. }
  66. break;
  67. case 'd':
  68. {
  69. int neg = 0;
  70. int dst = 0;
  71. int ch;
  72. int i;
  73.  
  74. trim_leading();
  75.  
  76. for ( i = 0; ( ch = getchar() ) != EOF; i++ ) {
  77. if ( i == 0 && ( ch == '-' || ch == '+' ) ) {
  78. neg = ( ch == '-' );
  79. continue;
  80. }
  81.  
  82. if ( !isdigit ( ch ) ) {
  83. ungetc ( ch, stdin );
  84. break;
  85. }
  86.  
  87. dst = 10 * dst + ( ch - '0' );
  88. }
  89.  
  90. if ( neg )
  91. dst = -dst;
  92.  
  93. *va_arg ( args, int* ) = dst;
  94. ++converted;
  95. }
  96. break;
  97. }
  98. }
  99. }
  100.  
  101. return converted;
  102. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC