How to clear input buffer? (FROM)

What he says on the tin. Is there a way to disable the input buffer to prepare for further user input without using undefined fflush (stdin) or looped getchar ()?

I am trying to write a small input function that is not vulnerable to buffer overflows. For this, I used fgets (typed_text, 30, stdin); which works as promised.

However, in case of an overflow, I have material left in the input buffer, which is read when I call the next input function. I want to clear the buffer to avoid this.

fflush (stdin) is supported but not defined. http://c-faq.com/stdio/stdinflush2.html suggests using the getchars loop, but the problem is that if the input buffer is empty, it will request a character and force me to press two "enter".

Is there a way to check if the input buffer is empty or delete it in other ways?

Thanks in advance.

+4
source share
2 answers

You can check if the input is empty using select().

Some related information is here: C select () timeout STDIN single char (no ENTER)

+2
source

fpurge . , man- fpurge .

0

Source: https://habr.com/ru/post/1526909/


All Articles