Help function fread library C

0

Can anyone tell me why this snippet of code just starts reading from the second element of the file?

And how could I do to read all the records in the file?

Code that I'm trying to use to read a txt file

 fread(vPtrContato, sizeof(contato_ref), 1, arquivo);
            printf("Nome: %s\n", contato_ref.primeiroNome);
            printf("Sobrenome: %s\n", contato_ref.segundoNome);
            printf("Telefone: %s\n", contato_ref.numeroTelefone);
            printf("E-mail: %s\n", contato_ref.email);
            fclose(arquivo);
    
asked by anonymous 16.12.2018 / 16:28

2 answers

0

Your fgetc () in the scan already advances to the next element, instead I recommend that you use:   while(feof(arquivo)){}

rewind () back to the beginning of the file, fseek () is a better alternative but a little more complicated to use.

    
16.12.2018 / 18:42
0

I was able to read from the first element using the rewind

while ((caracteres = fgetc(arquivo)) != EOF) {

        rewind(arquivo);
        fread(vPtrContato, sizeof (contato_ref), 1, arquivo);
        printf("Nome: %s\n", contato_ref.primeiroNome);
        printf("Sobrenome: %s\n", contato_ref.segundoNome);
        printf("Telefone: %s\n", contato_ref.numeroTelefone);
        printf("E-mail: %s\n", contato_ref.email);
        printf("==================================\n");

        fclose(arquivo);  }
    
16.12.2018 / 17:08