Clear C buffer with fflush() or __fpurge()

Asked

Viewed 5,080 times

5

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

Why use the function fflush() 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?

3 answers

8

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 expected of it, especially with stdin which is used by scanf(). It is a function to download streams output, contrary to the scanf() does, at least according to the specification.

If I’m not mistaken, just 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 is better to follow the specification and not a specific implementation.

The scanf() is to make something very simple, should not be used in anything serious, everyone who uses C in real way uses some other library or does its own function to handle data input. Whenever doing something sophisticated avoid this function, even in exercises, look for another way when it passes the very simple input. See more in How to read from stdin in C?.

6


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

So one thing you have to keep in mind is that: the use of the standard input in C, is with scanf or with other functions is a palliative while learning to program and for small exercises. A serious and large system, even if it has its core in "pure" C, should use an interface or framework library to create the interface - such as gobject, or to C++, boost, or Qt. Possibly it 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 quite counterintuitive for beginners. It only reads the tokens you send - and if there’s anything left in stdin, there’s something left in stdin. And there will always be at least the \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 just the fgets if you only want a string).

5

According to this link - in English:

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

In this response in the OS (in English), there are several solutions, such as 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 answer, using only looping while, do not seem safe as they depend on the character input '\n' to get out of the loop.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.