One , Function of buffer

* The general purpose of buffer is to prevent frequent read and write .

* A: Write in the buffer , There will be a write waiting time , There's not always data to write ;
* B: Read from buffer , There is a reading room here ( No data or incomplete data ) Waiting time for ;
Using a buffer can reduce the waiting time for program input , Program threads can free up CPU Time for other work ;

Two , Buffer problems

Buffer improves program efficiency at the same time , There are also some problems .

In the preparation of the project , Most of the time, user input is not required , In this case, if the user enters , The input information will be stored in the buffer , When the program really needs input , These were previously entered information , Will be passed into the program . Cause program to get error information , Even collapse .

Three , Solution

1. use fflush(stdin)

* advantage : Use this method to clear the buffer , Simple and fast , Just put this statement in front of the program's input statement .
* defect : But this method has a lot of flaws , Because in c This usage is not specified in the language standard , So not all compilers support it .

* If the compiler supports , Then clearing the buffer is easy .
* If the compiler does not support , It doesn't report a mistake , It's just after the statement is executed , The contents of the buffer still exist .
2.getch() Continuous acquisition of
while(getchar()!='\n') continue;
* advantage : Use this method to clear the buffer , Good versatility , Supported by any system and compiler .
* defect : Incomplete buffer cleanup , This buffer cleanup method , According to the judgment, the character obtained is “ Line break ” To stop .

* If there is a line break in the buffer , But not the last character , This is before the first newline character in the buffer ( Include line breaks ) Be cleared , But the characters after the first line break are not cleared .
* If there is no line break in the buffer , that while It's going to be in a dead loop , The program will stop , Only press enter , The program runs backwards , This affects the user experience .
* When it is not possible to determine whether the buffer needs to be cleared , This method cannot be used , This will cause the program to pause for no reason and wait for the user to enter the Enter key
3.setbuf()

* principle : send stdin Input stream changes from default buffer to no buffer .
* advantage : Use this method to clear the buffer , Simple and fast , Just put this statement in front of the program's input statement .
* defect : Poor versatility , This method is only applicable to Windows Most compilers for ,linux and Windows A small number of compilers used without response .
4.scanf(“%*[^\n]%*c”)

principle :%*
〔^\n〕 The ’\n’ Other characters before characters ,% hinder * Indicates that these characters read will be discarded , Met before ’\n’ Stop reading on character , here , There is one more in the buffer ’\n’ Character legacy , So the back one %*c This legacy newline character is read and discarded , The asterisk here has the same effect as the preceding asterisk . Since all input from the keyboard ends with a carriage return , And a carriage return produces one ’\n’ character , So will ’\n’ After all the characters before it are read and discarded , Which is equivalent to clearing the input buffer .

* advantage : Use this method to clear the buffer , Good versatility , Supported by any system and compiler .
* defect : Incomplete buffer cleaning , This buffer clearing method , According to the judgment, the character obtained is “ Line break ” To stop .

* When it is not possible to determine whether the buffer needs to be cleared , This method cannot be used , This will cause the program to pause for no reason and wait for the user to enter the Enter key
5. Judging keyboard input
while (_kbhit()) { getch(); }
kbhit() It's a C and C++ function , For non blocking response to keyboard input events .

This is a clear buffer method that I found myself :
* use kbhit() Determine whether there is keyboard input , That is to determine whether there is content in the buffer .
* If the buffer has content , Just use it getch() To get , Until all input in the buffer is cleared .

ps: Use this method , Simple and fast , Just copy these lines before the required input code , Dealing with all kinds of weird input from users , Can be cleaned up . At the same time, it can automatically determine whether the cleaning operation is needed , Avoid unexpected stop of program . It works !!!

Technology
©2019-2020 Toolsou All rights reserved,
( Essence )2020 year 7 month 15 day Wechat applet assembly Component Use of ( Essence )2020 year 6 month 26 day C# Class library GUID Help class Output string at specified position ( Detailed analysis ) Unity3D Input Key system 【Python- data fetch 】 read txt Each line of file generates a list of data python primitive -- lock Locklatex Custom commands in ———\newcommandajax get Request Chinese parameter garbled solution vue Value transfer between parent-child and non parent-child components python in switch_to_alert Usage of