C empty the input buffer and the use of Fflush (stdin) and the workaround

Source: Internet
Author: User
Tags rewind stdin

For C-language beginners, the fflush (stdin) function is interpreted as a system function that empties the input buffer, which is once almost half the story, with the progress of computer science, the gradual improvement in the process of learning will Fflush (stdin) The past and the present analysis of the function. Personal thinking:

Fflush (stdin) clears the contents of the input buffer and the contents of the input buffer are taken away by the scanf function, and under normal case the scanf () function can judge the number of successful fetches based on the return value; When an exception is read, an integer should be read, Result the current content in the input buffer is a string and a read exception occurs. After a read exception occurs, the contents of the input buffer are not taken away, and the next time the scanf () function discovers that there is content in the input buffer (it is obvious that the compiler does not care if the content is not legitimate), so it stops waiting for user input and tries to read directly from the input buffer The obvious is another read exception, so repeated.

Include <stdio.h>
int main (void)
{
    int val,ret;
    while (stdin), (ret = scanf ("%d", &val))!= EOF (fflush)
         printf ("%d\n", Val);
    return 0;
}

Intuitively, the program uses Fflush (STDIN) to ensure that the next read is not interfered when reading an exception. So the question is, is it true?

Fflush (stdin) in the past and present life
It is also clearly stated in the MSDN documentation provided by Microsoft: Fflush on input stream was an extension to the C standard (Fflush operation input Stream is the extension of the C standard, note that it is Extensi On )。 That is, the C standard does not define FFLUSH (stdin), the latest C 11 directly deleted the once playing the edge of the Fflush (stdin).
That's why the beginning of Fflush (stdin) has almost halved: fflush () is indeed an extension, which is almost right, and in the previous version of VS 2013, including VC + + 6.0 fflush (stdin) did work. Now even in the VS 2015 Environment, Fflush (stdin) no longer works, let alone the LUnix system, which is once to half.
Of course, if you don't care about the portability of the program, it's not a big problem to write in a version that can be Fflush (stdin). an alternative way to empty the input buffer

Since Fflush (stdin) now does not work under VS 2015, there must be a replacement for the role of the back pot, to achieve the role of emptying the buffer, the following according to the results of the lookup, given two of the C can be implemented to empty the input buffer function of a feasible solution.
First of all, the use of Setbuf (Stdin,null) is a method available under GCC, but there is no solution to the problem of caching, but there is no further scrutiny.
Under VS 2015, the following two methods can be used instead of fflush (stdin) to implement functionality:
1 Use Function rewind (stdin)
From the function name, this function should be to redefine the input buffer of the location or Size, this method to get the new buffer, before and after the two input buffer is not the same (pure guessing, easy hit face. In fact, this function seems to be a non-standard definition (not very sure, because in the C 11 and C 99 Update is not really see this, but also can be used as a temporary half. Here, to say a word, the C 11 standard does delete the gets () function, replace it with gets_s (), and put it in the last link.

2 using scanf ("%*[^\n]%*c"), the principle is to use the scan set to read all the characters in the buffer to achieve the elimination of the input buffer action, the effect is very useful, but also cross-platform.
At first glance this thing is still a bit abstruse, give a detailed explanation, also can refer to the explanation of the link inside.
Explain to scanf ("%*[^\n]%*c"):
%(^\n) will read the other characters before the ' \ n ' character in the buffer,% later indicates that the read characters are discarded and the read operation is stopped before the ' \ n ' character is encountered, and there is still a ' \ n ' character left in the buffer, so later%*c will read and discard the left line break. The asterisk here works the same as the preceding asterisk. Since all input from the keyboard ends with a carriage return and a carriage return produces an ' \ n ' character, it is equivalent to clearing the input buffer after reading and discarding ' \ n ' with its preceding characters. about Fflush (stdout)

If Fflush (stdin) is not considered, its brother Fflush (STDOUT) has a significant effect, in short, fflush (stdout) forces the output of the current output buffer, Some inexplicable error in debug can be fflush (stdout) immediately output in the process of intermediate results to determine the error location.
A sentence was found during the lookup:
Fflush (stdin) to the inflow operation is undefined, so this should be used with caution, may have side effects.
Fflush (stdout) simply outputs the contents of the output buffer that needs to be output, which facilitates debugging and has no adverse consequences.

In any case, fflush (stdin), like the diodes of the year, has been widely used in the development of computers. Looking back has been hundred years, perhaps later in the learning process will never meet fflush (stdin). reference materials:

1.iso/iec 9899:1999 (E) programming languages-c 7.19.5.2 the Fflush function
2.The C Programming Language 2nd Edition by Kernighan & Ritchie RELATED links:

scanf ("%[^\n]%*c") how to understand
SCANF and caching
C language Format input function scanf () detailed
Replace Fflush (stdin) with Rewind (stdin)
C99 and the newest C11 in the C language standard

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.