title: reading data from binary file record by record
body: <p>I'm trying to make a database that holds some datas about teams for now. The problem is that I cannot read the entered datas record by record. I want to explain it with just one example:</p>
  
  <pre><code>insert manunited,manchester,old_trafford,1878,black-rd to teams
  insert chelsea,london,stamford_bridge,1905,blue-whte to teams
  select colors,team_name,founding_date from teams
  </code></pre>
  
  <p>select prints on screen the specified information i.e. colors,stadium etc.</p>
  
  <p>insert takes teams information for now.So, According to the select command, output should be followed:</p>
  
  <pre><code>black-rd   manunited    1878
  blue-whte  chelsea      1905
  </code></pre>
  
  <p>But, I get </p>
  
  <pre><code>black-rd   manunited    1878   blue-whte
  </code></pre>
  
  <p>I've been trying to analyse my error for hours. I cannot find the error(s) and mistake(s).
  Thank you for all appreciated answers. By the way, I haven't maden while loop yet to insert or select commands. I'm pondering to find the error(s).</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  typedef struct
  {
      char team_name[TEAM_NAME];
      char city[TEAM_NAME];
      char stadium[STADIUM_NAME];
      int  founding_date;
      char colors[COLOR];
  }teams;
  
  int main()
  {
  
      char read_command[12];
      /*      checking insert     */
      char str1[100],str2[5],str3[18];
      FILE *fptr;
      teams t;
  
      scanf("%s", read_command);
  
      if(strcmp(read_command,"insert") == 0)
      {
          scanf("%s %s %s", str1, str2, str3);
          //printf("%s\n%s\n%s",str1,str2,str3);
          insertfunc(fptr,str1, str2, str3);
  
      }
      if(strcmp(read_command,"select") == 0)
      {
          scanf("%s %s %s", str1, str2, str3);
          selectfunc(fptr,str1, str2, str3);
      }
  
      return 0;
  }
  
  void selectfunc(FILE *fptr,char *str1,char *str2,char *str3)
  {
      char *buff;
      buff =  (char*) malloc(strlen(str1) + 1);
      strcpy(buff,str1);
      char *token;
  
      const char comma[2] = ",";
  
      if(strcmp(str3,"teams") == 0)
      {
          teams t;
  
          fptr = fopen("teams.bin","rb");
  
          if( fptr == NULL )
          {
              perror("File cannot be opened.");
              exit(1);
          }
          else
          {
              while(fread(&amp;t,sizeof(teams),1,fptr) == 1)
              {
                  /* get the first token */
                  token = strtok(buff, comma);
  
                  while( token != NULL )
                  {
                      if(strcmp(token,"team_name") == 0)
                      {
                          printf("  %s  ",t.team_name);
                      }
                      else if(strcmp(token,"city") == 0)
                      {
                          printf("  %s  ",t.city);
                      }
                      else if(strcmp(token,"stadium") == 0)
                      {
                          printf("  %s  ",t.stadium);
                      }
                      else if(strcmp(token,"colors") == 0)
                      {
                          printf("  %s  ",t.colors);
                      }
                      else
                      {
                          printf("  %d  ",t.founding_date);
                      }
  
                      token = strtok(NULL, comma);
                  }
              }
          }
          fclose(fptr);
      }
  
  
  }
  
  
  void insertfunc(FILE *fptr,char *str1,char *str2,char *str3)
  {
      char *buff;
      buff =  (char*) malloc(strlen(str1) + 1);
      strcpy(buff,str1);
  
      const char comma[2] = ",";
  
      /*
      if(buff == NULL)
          perror("error");
      */
  
      if(strcmp(str3,"teams") == 0)
      {
          teams t;
          int date;
  
          strcpy(t.team_name,strtok(buff, comma));
          strcpy(t.city, strtok(NULL, comma));
          strcpy(t.stadium, strtok(NULL, comma));
          date = atoi(strtok(NULL, comma));
          t.founding_date = date;
          strcpy(t.colors, strtok(NULL, comma));
  
          fptr = fopen("teams.bin","ab+");
  
          if( fptr == NULL )
          {
              perror("File cannot be opened.");
              exit(1);
          }
          else
          {
              fwrite(&amp;t,sizeof(teams),1,fptr);
              fclose(fptr);
          }
      }
      free(buff);
  }
  </code></pre>