title: reading a csv file into struct array
body: <p>I'm beginning to code in C. My code is as follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #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)&gt;0) &amp;&amp; (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 &lt;= 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;
      }
  
  }
  </code></pre>
  
  <p>The problem is, that after readBookFile() ends, the Array of my struct is full of the last value of the input file.. </p>
  
  <p>My input file is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>so in the readBookFile function the printf returns the correct values, but in the printBooksList() function all values seem to have changed to the last line of my inputfile.</p>
  
  <p><img src="https://i.stack.imgur.com/cip5I.png" alt="the output on my console"></p>
  
  <p>Can anyone explain this to me and maybe point me in the right direction?</p>
  
  <p>Thanks a lot
  Hagbart</p>