title: C: How to update file with new information in inserted into a structure
body: <p>I have a program below that acts like a library manager to help organize my books collection but somehow when I update the file with new information when I exist the program, the information in the file is is deleted and nothing is saved into the file. </p>
  
  <p>What this program does is add new books into a structure and then when the user types in 8 to exit the program the information from the structure is saved into file and when the program is run the next time, the information from the file is initialized back into the structure. Somehow my implementations is not working right or somewhere I am doing wrong that needs another pair of eyes to see.</p>
  
  <pre><code>#define MAX 100
  #define OUT 1
  #define IN  0
  #define RETURNTIME 30
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  
  struct date
  {
      int nYear, nMonth, nDay;
  };
  
  struct BOOK {
      int     nID;
      char    chTitle[50];
      char    chType[50];
      char    chPublisher[50];
      int     nPages;
      float   nPrice;
      int     nPubYear;
      int     nStatus; //0=available, 1=checked-out
      char    userName;
      struct  date issued, due;
  }BOOK[100];
  
  FILE *fp;
  FILE *fpin;
  
  //Function calls
  void addBookFunction();
  void checkOutBookFunction();
  void checkInBookFunction();
  void displayAllBookFunction();
  void displayAllAvailableBookFunction();
  void displayAllCheckedOutBookFunction();
  
  int checkID(int t);
  
  int count;
  
  int main(int argc, const char * argv[])
  {
      int nInput;
      char buffer[100];
      char *token;
  
      fpin = fopen("books.txt", "r");
      if (fpin == NULL)
      {
          printf("Unable to open the file");
          exit(0);
      }
      while (!feof(fpin))
      {
          fgets(buffer, sizeof(buffer), fpin);
          int i = 1;
          token = strtok(buffer, ",-");
          while (token != NULL)
          {
              if (i == 1)
              {
                  BOOK-&gt;nID = atoi(token);
              }
              if (i == 2)
              {
                  strcpy(BOOK-&gt;chTitle, token);
              }
              if (i == 3)
              {
                  strcpy(BOOK-&gt;chType, token);
              }
              if (i == 4)
              {
                  strcpy(BOOK-&gt;chPublisher, token);
              }
              if (i == 5)
              {
                  BOOK-&gt;nPages = atoi(token);
              }
              if (i == 6)
              {
                  BOOK-&gt;nPages = atoi(token);
              }
              if (i == 7)
              {
                  BOOK-&gt;nPubYear = atoi(token);
              }
              if (i == 8)
              {
                  BOOK-&gt;nStatus = atoi(token);
              }
              if (i == 9)
              {
                  BOOK-&gt;due.nYear = atoi(token);
              }
              if (i == 10)
              {
                  BOOK-&gt;due.nDay = atoi(token);
              }
              if (i == 11)
              {
                  BOOK-&gt;due.nMonth = atoi(token);
              }
              token = strtok(NULL, ",-");
              i++;
          }
      }
  
      while (1)
      {
          printf("\n\t\t~~MENU~~\n");
          printf("1. Add a new book\n");
          printf("2. Remove a book (input ISBN)\n");
          printf("3. Check out a book (input ISBN)\n");
          printf("4. Check in a book (input ISBN)\n");
          printf("5. Display all book details\n");
          printf("6. Display all available book details\n");
          printf("7. Display all checked out books\n");
          printf("8. Exit program\n");
          printf("Make your selection: ");
          scanf("%d", &amp;nInput);
  
          switch (nInput)
          {
              case 1:
                  addBookFunction();
                  getchar();
                  break;
              case 2:
                  break;
              case 3:
                  checkOutBookFunction();
                  break;
              case 4:
                  checkInBookFunction();
                  break;
              case 5:
                  break;
              case 6:
                  //Display_all_Available_Book_Details();
                  break;
              case 7:
                  displayAllCheckedOutBookFunction();
                  break;
              case 8:
                  printf("\nGoodby!!! Thanks for using The Libray Manager\n");
                  fp = fopen("books.txt", "ab+");
                  fseek(fp, 0, SEEK_END);
                  for (int j = 0; j&lt;count; j++)
                  {
                      fprintf(fp, "%d, %s, %s, %s, %d, %0.2f, %d, %d, %d-%d-%d\n", BOOK[j].nID, BOOK[j].chTitle, BOOK[j].chType, BOOK[j].chPublisher, BOOK[j].nPages, BOOK[j].nPrice, BOOK[j].nPubYear, BOOK[j].nStatus, BOOK[j].due.nYear,BOOK[j].due.nDay,BOOK[j].due.nMonth);
                  }
                  fclose(fp);
                  exit(1);
                  break;
              default:
                  printf("\n\t\t~~Invalid Input~~\n");
                  break;
          }
      }
      return 0;
  }
  
  void addBookFunction()
  {
      int bookId;
      if (count == 9)
      {
          printf("\n no more spaces\n");
          return;
      }
      printf("\n Enter Book Details\n");
      printf("Enter book ISBN: ");
      scanf("%d", &amp;bookId);
      if (BOOK[count].nID == bookId)
      {
          printf("\nSorry another book with that id: Try again!\n");
          addBookFunction();
      }
      else
      {
          BOOK[count].nID = bookId;
          printf("Enter book title: ");
          scanf("%s", BOOK[count].chTitle);
          printf("Enter book type (eg. magazine, novel): ");
          scanf("%s", BOOK[count].chType);
          printf("Enter book publisher (eg. UTA): ");
          scanf("%s", BOOK[count].chPublisher);
          printf("Enter book's number of pages: ");
          scanf("%d", &amp;BOOK[count].nPages);
          printf("Enter book's price: ");
          scanf("%f", &amp;BOOK[count].nPrice);
          printf("Enter year published: ");
          scanf("%d", &amp;BOOK[count].nPubYear);
          BOOK[count].nStatus = IN;
          count++;
      }
  }
  
  void checkOutBookFunction()
  {
      int nInput, i = 0;
      char buffer[5];
  
      printf("Enter the ISBN: ");
      scanf("%d", &amp;nInput);
      if (checkID(nInput) == 0)
      {
          time_t now;
  
          if ( time(&amp;now) != (time_t)(-1) )
          {
              struct tm *d = localtime(&amp;now);
              if ( strftime(buffer, sizeof(buffer), "%Y", d) )
              {
                  BOOK-&gt;issued.nYear = atoi(buffer);
                  BOOK-&gt;issued.nMonth = d-&gt;tm_mon;
                  BOOK-&gt;issued.nDay = d-&gt;tm_mday;
              }
          }
          BOOK-&gt;due.nDay = BOOK[i].issued.nDay+RETURNTIME;
          BOOK-&gt;due.nMonth = BOOK[i].issued.nMonth;
          BOOK-&gt;due.nYear = BOOK[i].issued.nYear;
  
          if (BOOK-&gt;due.nDay &gt; 30)
          {
              BOOK-&gt;due.nMonth += BOOK-&gt;due.nDay/30;
              BOOK-&gt;due.nDay -= 30;
          }
          if (BOOK-&gt;due.nMonth &gt; 12)
          {
              BOOK-&gt;due.nYear += BOOK-&gt;due.nMonth/12;
              BOOK-&gt;due.nMonth -=12;
          }
  
          printf("Issued date = %d-%d-%d\n", BOOK-&gt;issued.nYear,BOOK-&gt;issued.nMonth, BOOK-&gt;issued.nMonth);
          printf("Return date = %d-%d-%d", BOOK-&gt;due.nYear, BOOK-&gt;due.nMonth, BOOK-&gt;due.nDay);
  
          BOOK[i].nStatus += OUT;
      }
      else{
          printf("No book with that id");
      }
  }
  
  void checkInBookFunction()
  {
      int nInput, i;
      printf("Enter the ISBN: ");
      scanf("%d", &amp;nInput);
      if (checkID(nInput) == 0)
      {
          printf("Book Found");
          BOOK[i].nStatus = IN;
      }
  }
  
  void displayAllBookFunction()
  {
  }
  
  void displayAllAvailableBookFunction()
  {
  }
  
  void displayAllCheckedOutBookFunction()
  {
  
      if (BOOK-&gt;nStatus == OUT)
      {
          printf("%d, %s", BOOK-&gt;nID, BOOK-&gt;chTitle);
      }else{
          printf("\n\t\t~~NO BOOKS HAS BEEN CHECKED OUT~~\n");
      }
  }
  
  int checkID(int t)
  {
      int i;
      while (i &lt; MAX)
          if (BOOK[i].nID == t)
              return 0;
      return 1;
  }
  </code></pre>