How to empty buffers under Linux

Source: Internet
Author: User
Tags stdin

The method of emptying buffers under Linux C standard specifies that the Fflush () function is used to refresh the output (stdout) cache. For input (stdin), it is undefined. But some compilers also define the implementation of Fflush (stdin), such as Microsoft's VC.    Whether other compilers also define the Fflush (stdin) implementation should look for its manual. The GCC compiler does not define its implementation, so you cannot use Fflush (stdin) to flush the input cache.    For compilers that do not have a fflush (stdin) defined, you can use the fgets () function instead (more versatile than GetChar (), scanf (), and so on.    Where the end flag for scanf is a carriage return or a space or tab, and the end of the get can only be carriage return line.    The standard input buffers need to be emptied during Linux programming, so the following methods are used in Windows programming. 1, Fflush (stdin); this fflush () function is not a function in standard C at all, just an extension of standard C, so it is not possible to use it in Linux;2, online search, found that a netizen suggested the use of Rewind (stdin); This function is actually pointing the pointer at the beginning of the stream.    But it is a function in the file operation, the operation is the type stream, in Windows programming can be emptied of the standard input buffer, but not in Linux. 3, by reading the remaining characters in the standard buffer and discarding it to empty the standard buffer, the function used is GetChar (), the function of which is to read a character from the standard input buffer, which is feasible in Linux. If you need to clear stdin, you can do this through the following loops:CharCh while((Ch=getchar ())! ='\ n'&&ch!=EOF); The above statement clears the characters in the stdin, knowing that a newline character is encountered or that the buffer is read out. The above methods can be used in Windows programming to clear the buffer;4There is also a method for emptying stdin in Linux programming, calling the following function directly: Setbuf (stdin, NULL); This function I haven't had time to test; with the above analysis, there are two ways to empty stdin under Linux:1. By reading the remaining characters and discarding them;CharCh while((Ch=getchar ())! ='\ n'&&ch!=eof); or:Chars[1024x768];fgets (s),1024x768, stdin); 2. Use the function setbuf (stdin,null);
Reprinted from: http://m.blog.csdn.net/blog/yanbober/8720542

How to empty buffers under Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.