czytanie pliku csv do tablicy struct

Zaczynam kodować C. Mój kod jest następujący:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STR_LEN 256
#define MAX_BOOKS 256

struct book{
    int ID;
    char *name;
    char *dateIn;
    char *dateOut;
};

struct book books[MAX_BOOKS];

/* PROTOTYPE OF FUNCTIONS */
int readBookFile();
void printBookList();


int main(int argc, char **argv)
{   
    int isOK = 0;

    isOK = readBookFile();

    printBookList();

    system("pause");
    return 0;
}

int readBookFile()
{
    /* FileStream for the Library File */
    FILE *bookFile;

    /* allocation of the buffer for every line in the File */
    char *buf = malloc(MAX_STR_LEN);
    char *tmp; 

    /* if the space could not be allocaed, return an error */
    if (buf == NULL) {
        printf ("No memory\n");
        return 1;
    }

    if ( ( bookFile = fopen( "library.dat", "r" ) ) == NULL ) //Reading a file
    {
        printf( "File could not be opened.\n" );
    }

    int i = 0;
    while (fgets(buf, 255, bookFile) != NULL)
    {
        if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n'))
            buf[strlen (buf) - 1] = '\0';       

        tmp = strtok(buf, ";");
        books[i].ID = atoi(tmp);

        tmp = strtok(NULL, ";");
        books[i].name = tmp;

        tmp = strtok(NULL, ";");
        books[i].dateIn = tmp;

        tmp = strtok(NULL, ";");
        books[i].dateOut = tmp;

        //tempBook.ID = atoi(buf);
        printf("index i= %i  ID: %i, %s, %s, %s \n",i, books[i].ID , books[i].name, books[i].dateIn , books[i].dateOut);

        i++;
    }
    //free(buf);
    fclose(bookFile);
    return 0;
}

void printBookList()
{

    int i;
    //i = sizeof(books) / sizeof(books[0]);
    //printf ("%i \n", i);


    for (i = 0; i <= sizeof(books); i++)
    {
        if (books[i].ID != 0)
        printf("index i= %i  ID: %i, %s, %s, %s \n",i, books[i].ID , books[i].name, books[i].dateIn , books[i].dateOut);
        else
            break;
    }

}

Problem polega na tym, że po zakończeniu readBookFile () tablica mojej struktury jest pełna ostatniej wartości pliku wejściowego.

Mój plik wejściowy to:

1;das erste Buch; 12122013; 13122013
2;das Zweite Buch; 12122013; 13122013
3;das dritte Buch; 12122013; 13122013
4;das vierte Buch; 12122013; 13122013
5;das fünfte Buch; 12122013; 13122013
6;das sechste Buch; 12122013; 13122013

więc w funkcji readBookFile printf zwraca poprawne wartości, ale w funkcji printBooksList () wszystkie wartości wydają się zmienić na ostatnią linię mojego pliku wejściowego.

Czy ktoś może mi to wyjaśnić i może wskazać mi właściwy kierunek?

Wielkie dzięki Hagbart

questionAnswers(3)

yourAnswerToTheQuestion