Error while clearing buffer in GCC (Ubuntu 18.04)

Asked

Viewed 458 times

2

I was watching a tutorial of C, and appeared a part about buffer cleaning using the functions fflush and __fpurge. So far so good, but when I tried applying with __fflush, the GCC returns me the following error:

warning: implicit declaration of function ‘__fpurge’; did you mean ‘__wur’? [-Wimplicit-function-declaration]
  __fpurge(stdin); // limpa buffer de entrada

And in trying to use the fflush, does not perform cleaning. How to proceed?

Code using fflush:

#include<stdio.h>

int main(){
        char letra1,
             letra2;

        // uso da função getc(metodo_de_entrada)
        printf("Insira um caractere: \n");
        letra1 = getc(stdin);

        fflush(stdin); // limpa buffer de entrada

        printf("Insira outro caractere: \n");
        letra2 = getc(stdin);

        printf("Voce digitou: '%c' e '%c'\n", letra1, letra2);
        return 0;
}

Code using __fpurge:

 #include<stdio.h>

int main(){
        char letra1,
             letra2;

        // uso da função getc(metodo_de_entrada)
        printf("Insira um caractere: \n");
        letra1 = getc(stdin);

        __fpurge(stdin); // limpa buffer de entrada

        printf("Insira outro caractere: \n");
        letra2 = getc(stdin);

        printf("Voce digitou: '%c' e '%c'\n", letra1, letra2);
        return 0;
}

I’m using Ubuntu 18.04 and GCC 7.4.0

  • 5

    fflush has undefined behavior when applied to input streams (stdin). See issue 12.26a of http://www.faqs.org/faqs/C-faq/faq/. See: https://answall.com/questions/111697/limpar-buffer-em-c-com-fflush-ou-fpurge#111703

  • Just for the record, in the code you show in the question there is no need to clean the buffer.

2 answers

3


In general it is not necessary to "clean" input buffers, normal input/output functions are sufficient for all needs.

In addition the function fflush for stdin is not standard, does not work on Linux, for example.

And __fpurge is even worse, as it has two characters _ at first, this indicates that it is an internal function of the C library, should not be used by application programs.

Tips:

  1. input of numerical fields data (%d, %f etc) ignores initial spaces and end-of-line characters (\r and \n)

  2. string data input (%s) ignore initial spaces and end-of-line characters (\r and \n)

  3. character data input (%c) DO NOT ignore starting spaces and end-of-line characters (\r and \n), but this is easily bypassed using a format " %c", that is, with a space before the %c; this previous space "eats" all spaces, tabs and end of line, before reading the next character

2

After reading the article the user anonimo left in the comments, and to access this page, I came across some alternatives to perform the cleaning I needed (and make the computer "forget" that I pressed ENTER after typing a char).

The first one was using the function scanf, which comes as standard in the library stdio.h:

scanf(" %c", &letra2); // inserindo um "espaço" entre as primeiras aspas duplas e o '%c'

Another way to do the same is by inserting a getchar "ghost" (no variable receiving it) after typing something:

letra1 = getc(stdin);
getchar();

Another way there is is by using scanf("%c%*c", &letra1);, but I have not tested this form, so I can say nothing about its effectiveness.

Browser other questions tagged

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