Help fread C library function

Asked

Viewed 112 times

0

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

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

Code 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);

2 answers

1


Your fgetc() in the check already moves on to the next element, instead I recommend you use: while(feof(arquivo)){}

Rewind() back to the beginning of the file, fseek() is a rather more complicated alternative to be used.

0

I was able to read from the first element using the function 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);  }

Browser other questions tagged

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