Clear C buffer with fflush () or __fpurge ()

3

Studying strings in C I came across the following phrase: "Cleaning the buffer, for example, is not always desirable, and for more professional and safe programming it is not recommended to use fflush for example."

Why using the fflush() function to clear the buffer after a scanf() is not recommended? What is the best way to clear the buffer to avoid problems like reading the NEW LINE character unintentionally?

    
asked by anonymous 04.02.2016 / 15:24

3 answers

3

It is best to understand that scanf is a function written in the 70's to read tokens from streams. It may be convenient to extract integers, or strings in an interactive program - but it comes from a time when interactive programs were Well less interactive than those of today. Even programs designed to work only from the command line today (for example, git ) have a sophisticated interface compared to what the scanf lets you do.

So one thing to keep in mind is that using standard input in C, whether with scanf or with other functions is a palliative while learning to program and for small exercises. A serious, large system, even if it has its "pure" C core, must use an interface library or framework to create the interface - such as gobject , or C ++, boost , or Qt . Possibly you can even have all the interface part written in a high level language, and only the internal loops, where "brute force" is needed done in C.

That said - scanf is very counterintuitive for beginners. He just reads the tokens you send - and if there's anything left in the stdin, there's nothing left in the stdin. And there will always be at least \n , unless you read a token %s or last. An alternative is to use scanf followed by fgets that always reads a string to the end (or only fgets if you only want a string).

    
05.02.2016 / 13:22
5

The main reason is that there is no clear definition of the ISO standard. Few compilers implement it in a way that does what is usually expected of it, especially with stdin that is used by scanf() . It is a function to unload streams , the opposite of what scanf() does, at least according to the specification.

If I'm not mistaken, only the Microsoft compiler, among the best known, works. So if you decide to use it, make sure the code never needs to be compiled into another compiler.

It's best to follow the specification rather than a specific implementation .

04.02.2016 / 15:58
3

According to this link - in English :

The function fflush() , works only on output buffers, and if you force it to use with an input buffer (eg stdin , used by scanf ), the result is undefined.

In this answer in the SO, there are several solutions, like this (best):

int c;

while ( (c = getchar()) != '\n' && c != EOF ) { }

or this (which according to the comments is not portable):

fseek(stdin, 0, SEEK_END);

The other options in the response, using only the while loop, do not seem to be safe since they depend on the input of the '\n' character to exit the looping.

    
04.02.2016 / 15:57