Mail Archives: djgpp/1999/04/07/16:33:27
> In TurboC you MUST do fflush(stdin) before you can use gets, because
> the buffer must be empty(How can you empty the buffer in DJGPP?).
I think you're solving the wrong problem. You don't want to empty the
buffer, you just want to ignore the remainder of the line, right? If
so, there are two solutions:
1. use scanf("%*[^\n]\n"); This will read and discard all characters
up to and including the next newline (i.e. it ignores the rest of
the line).
2. (My choice) Use fgets to read the line, then parse it with sscanf.
Consider the case where you redirect input from a file. The "buffer"
is going to be the first 512 bytes of the file, *not* the first *line*
of the file. fflush(stdin) would toss all 512 bytes, probably leaving
you in the middle of some line down the list. The scanf solution
works for both cases.
Oh, and you should never use gets. Always use fgets. gets doesn't
have a way of stopping buffer overrun from long lines. fgets is
safer.
- Raw text -