How can I clean the buffer?

Asked

Viewed 776 times

2

I am working with file. I have difficulties cleaning the buffer already used the fflush(stdin) after reading but it doesn’t work.

void EscreverFicheiro(FILE * ptr,LISTA dados){

    ptr = fopen("listagem","wb");
    system("cls");
    printf("\n\t\tPOR FAVOR INSIRA SEUS DADOS\n\n");
    printf("\t\tNOME: ");
    fflush(stdin);
    gets(dados.nome);
    printf("\t\tRUA: ");
    fflush(stdin);
    gets(dados.rua);
    printf("\t\tCIDADE: ");
    fflush(stdin);
    gets(dados.cidade);
    printf("\t\tPROVINCIA: ");
    fflush(stdin);
    gets(dados.provincia);
    printf("\t\tCOD POSTAL: ");
    fflush(stdin);
    gets(dados.codpost);
    fflush(stdin);
    fwrite(&dados,sizeof(struct lista_type),1,ptr);

    fclose(ptr);
}
  • You used the CSS tag and you wanted to use C, right? Or is it C++?

  • is even in C...

  • I used a structure to gather information and store in the file...

1 answer

1

Alternatives to fflush:

fseek(stdin, 0, SEEK_END);

Or else:

while (getchar() != '\n');
  • Thanks! but I just found out that the error is not in the file but in the structure... I declared it as follows:

  • typedef struct lista_type{ char nome[40]; char rua[30]; char cidade[4]; char provincia[3]; char codpost[10]; }LIST

  • I believe your question has been answered. If you have other questions, please feel free to create new questions.

Browser other questions tagged

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