problem z odczytem struktur w C

0

Witam
Napisałem sobie program który czyta z pliku binarnego strukture, mam jednak problem tego typu, że ostatnia struktura wyświetlana jest 2 razy. Może zauwazy ktoś błąd z góry dzięki


```    if(a==1){
        FILE *plik=fopen("C://Documents and Settings//Patryk1//Pulpit//1.txt","rb");
          struct zakup lista;
          int i=1;
       while(!feof(plik)){
       fread(&lista,sizeof(struct zakup),1,plik);
            printf("produkt nr %d: %s\n",i,lista.nazwa);
          printf("cena: %.2f\n",lista.cena);
          printf("ilosc: %d\n",lista.ilosc);
          i++;}
          fclose(plik);
    }
1

Znacznik końca pliku ustawiany jest dopiero wtedy, gdy spróbujesz czytać poza końcem pliku.
Sprawdzaj, co zwróciło fread:

   RETURN VALUE

   On  success,  fread()  and  fwrite() return the number of items read or
   written.  This number equals the number of bytes transferred only  when
   size  is 1.  If an error occurs, or the end of the file is reached, the
   return value is a short item count (or zero).

   fread() does not distinguish between end-of-file and error, and callers
   must use feof(3) and ferror(3) to determine which occurred.

1 użytkowników online, w tym zalogowanych: 0, gości: 1