title: fread segmentation fault reading database from binary file in c
body: <p>I am writing a function, that helps me read a database of books from a binary file. However when using <em>fread</em> i am having a segmentation fault.</p>
  <p>This is the function I am talking about.</p>
  <pre><code>Array *readdb (FILE *db, Array *ind) {
      Array *database = NULL;
      Book *book = NULL;
      indexbook *indentry;
      char *buffer, bytesdepillar = 0, titprintby[MAX_ENTRADA];
      int reader = 0;
  
      if (db == NULL || ind == NULL) 
          return NULL;
  
      database = malloc(sizeof(Array));
  
      initArray(database, INIT_ARR_SIZE);
  
      fseek(db, 0L, SEEK_END);
      bytesdepillar = ftell(db)/sizeof(char);
      fseek(db, 0, SEEK_SET);
      buffer = malloc(bytesdepillar * sizeof(char));
      fread(buffer, sizeof(char), bytesdepillar, db);
      
      for (size_t i = 0; i &lt; ind-&gt;used; i++) {
          book = malloc(sizeof(Book));
          indentry = (indexbook*) ind-&gt;array[i];
  
          reader = indentry-&gt;offset;
  
          book-&gt;bookID = indentry-&gt;key;
          reader+=sizeof(int);
          memcpy(book-&gt;ISBN, &amp;buffer[reader], ISBN_LEN);
          reader+=ISBN_LEN;
          memcpy(titprintby, &amp;buffer[reader], indentry-&gt;offset + indentry-&gt;size - reader);
          strcpy(book-&gt;title, strtok(titprintby, &quot;|&quot;));
          strcpy(book-&gt;printedBy, strtok(NULL, &quot;|&quot;));
      
          insertArray(database, book);
      }
  
      return database;
  }
  </code></pre>
  <p>ind is the database index with the following structure:</p>
  <pre><code>typedef struct {
    int key;
    long int offset;
    size_t size; 
  } indexbook;
  
  </code></pre>
  <p>The file is being opened with</p>
  <pre><code>    if ((dbf = fopen(db_name, &quot;rb&quot;)) == NULL) {
          fclose(indf);
          printf(&quot;%s does not exist&quot;, db_name);
          return 1;
      }
  
  </code></pre>