title: C - fseek() affecting the contents(structure) of a file when trying to update the records
body: <p>Not sure what is wrong the fseek() in this function as I am lead to believe that it is the cause for the problems with the incorrect output for my program. </p>
  
  <pre><code>   index = 0;
      present = 0;
      while(fread(&amp;beverage, sizeof(struct beverages), 1, filepointer))
      {
          if(newFile == 0)
              break;
  
          printf("%s\n\n", beverage.bcode);
          getch();
  
          if(strcmp(_bev_name, beverage.bname) == 0)
          {
              printf("Inside if statement\n\n");
              getch();
  
              beverage.quantity = beverage.quantity + qty;
              beverage.qty_last_purchase = qty;
  
  
              priceOne = spent/qty;//Used to calculate the cost price for one beverage
              beverage.price = priceOne + (priceOne*0.10);//Calculation of sale price for beverage
  
              printf("%d\n\n", index);
              getch();
  
              fseek(filepointer,index*sizeof(struct beverages),SEEK_SET);
              fwrite(&amp;beverage,sizeof(struct beverages),1,filepointer);
  
              present = 1;
              break;
          }
          else
          {
              present = 0;
              index = index + 1;
          }
  
      }
  
      if(present == 0)
      {
          //Stores new beverage name
          strcpy(beverage.bname, _bev_name);
  
          //Creation of beverage code using the first two letters of beverage name and random 3 digit number
          srand(time(NULL));
          int ran_num = rand()%(999 + 1 - 100) + 100;
          char str[3];
  
          itoa(ran_num, str,10);
  
          beverage.bcode[0] = beverage.bname[0];
          beverage.bcode[1] = beverage.bname[1];
          beverage.bcode[2] = str[0];
          beverage.bcode[3] = str[1];
          beverage.bcode[4] = str[2];
          beverage.bcode[5] = '\0';
  
          beverage.qty_last_purchase = qty;
          beverage.quantity = qty;
          priceOne = spent/qty;//Used to calculate the cost price for one beverage
          beverage.price = priceOne + (priceOne*0.10);//Calculation of sale price for beverage
  
          fwrite(&amp;beverage, sizeof(struct beverages), 1, filepointer);
  
      }
  </code></pre>
  
  <p>The first part of the code loops through an existing file to find the matching beverage code and then to update the quantity and price of the beverage but what has been happening is that it is affecting the other structures in the file. </p>
  
  <p><a href="https://i.stack.imgur.com/MERdS.png" rel="nofollow noreferrer">Shows how beverages are displayed when they are first added to file</a></p>
  
  <p><a href="https://i.stack.imgur.com/6D3RY.png" rel="nofollow noreferrer">Shows how the list of beverages are affected after the records are updated</a></p>
  
  <p>To be noted I chose to update the first beverage in the first picture but as you can see it affects the second beverage record. </p>
  
  <p>EDIT : The definition of the struct beverages</p>
  
  <pre><code>struct beverages
  {
      char bname[NAME_LIMIT];
      char bcode[MAX_SIZE];
      int quantity;
      int qty_last_purchase;
      float price;
  } beverage;
  </code></pre>