-
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
-
-
i already see the code but i don't want the program halt
-
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. :D :D
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);
}
-
i'll try the code...
but i have to use readRestOfLine() to clear the buffer....
-
>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 %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.