Thread: clear buffer

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    8

    clear buffer

    i havin problem to clear the buffer this is the following condition

    Prompts being printed multiple times or your program "skipping" over prompts because left over input was accepted automatically. These problems are a symptom of not using buffer overflow code often enough.

    Program halting at unexpected times and waiting for the user to hit enter a second time. Calling your buffer clearing code after every input routine without first checking if buffer overflow has occurred causes this.

    Using fflush(stdin). This function is not appropriate for reasons mentioned
    Using rewind(stdin). This function is not appropriate as it is not intended to be used for buffer clearing.

    The use of buffer clearing code may have been avoided with gets() or scanf() for scanning string input. These functions are not safe because they do not check for the amount of input being accepted.

    Using long characters arrays as a sole method of handling buffer overflow. We want you to use the readRestOfLine() function.
    Other buffer related problems.

    For example, what happens in your program when you try to enter a string of 40 characters when the limit is 20 characters?

    Code:
    /**************************************
     * Function readRestOfLine() is used for buffer clearing.
     *************************************/
    void readRestOfLine(){
       int c;
       /* Read until the end of the line or end-of-file. */   
       while ((c = fgetc(stdin)) != '\n' && c != EOF)
          ;
       /* Clear the error and end-of-file flags. */
       clearerr(stdin);
    }
    thanks for the help

  2. #2

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    8
    i already see the code but i don't want the program halt

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    fseek() can be done on all streams - binary mode for binary streams (which the keyboard stream usually is for me), or in text mode, for text streams.

    This is according to K&R 2nd Edition, not me.

    Try this, and try to overstuff the buffer as much as you want.

    Code:
    /* Password.c Alpha ver. 1.02 by Adak */
    
    #include <stdio.h>
    #include <conio.h>  	/* for getch() */ 
    
    #define MaxChars 20
    char c;
    char Usrname[MaxChars] = { '\0' };
    char Passwrd[MaxChars] = { '\0' };
    
    int main()  {
       int j, i = 0;
       printf("\n\n Enter Username: ");
       fgets(Usrname, MaxChars, stdin);
    
       printf("\n Enter Password:");
       putchar(' ');
    
       while((c = getch()) != 13) {   /* '\n' doesn't work here */
          if (c > 31 && i < MaxChars - 1)  {
            putchar('*');
            Passwrd[i++] = c;
          }else {
            if ((i > 0) && i -1 < (MaxChars - 1) && c == 8) {
              putchar(c);
              putchar(32);
              putchar(c);
              Passwrd[--i] = 32;
            }
          }
       }  
       Passwrd[i] = '\0';
       /* display it */
       printf("\n\n       Username: %s", Usrname);
       printf("\n       Password: %s", Passwrd);
       printf("\n\n\t\t     Program Complete, Please Press Enter "); 
       fseek(stdin, 0, SEEK_END);
       c = getchar();
    
       return(0);
    }

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    8
    i'll try the code...

    but i have to use readRestOfLine() to clear the buffer....

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >These functions are not safe because they do not check for the amount of input being accepted.
    Actually, gets doesn't, but scanf does if you use the correct format modifier. Most people just use &#37;s, which is wrong. I noticed no mention of fgets though.

    >i already see the code but i don't want the program halt
    You can't just blindly use readRestOfLine. In fact, if you're doing your job properly, most of the time you don't need it. But let's assume that once the buffer is full, everything else in the stream is garbage, since that's what you seem to be saying is the case. Using fgets, if there's a newline in the buffer then a full line was retrieved and there's no need to call readRestOfLine. If there's no newline, the buffer was filled (or there was an error), and there are still pending characters in the stream:
    Code:
    if ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
      char *newline = strchr ( buffer, '\n' );
    
      /* Deal with a stream error here if you want */
    
      if ( newline == NULL )
        readRestOfLine();
    }
    >fseek() can be done on all streams - binary mode for binary streams (which the keyboard stream usually is for me), or in text mode, for text streams.
    fseek is heavily restricted for text streams though, and if we're talking about stdin then the stream isn't guaranteed to be seekable.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. Frame buffer not working
    By Noise in forum Game Programming
    Replies: 1
    Last Post: 02-15-2009, 12:05 PM
  3. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  4. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  5. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 04:46 PM