Q: Delphi rewriting .dat file I'm using Delphi and am attempting to rewrite a .dat file which stores records of books. One of the variables is .BookAmountAvailable, when a book is taken out I want it to lower to .BookAmountAvailable of that record by 1, I've read the file into a typed array, then found the correct book and lowered the amount available by 1.
When I rewrite the file with the updated array it doesn't write it correctly (it only writes one of the records back correctly, and the rest of the records are left blank.
The code to read into the array is:
assignfile (BookFile,'BookFile.dat');
reset (BookFile);
BookCounter := FileSize(BookFile);
SetLength(BookArray, BookCounter);
for Count1 := 1 to BookCounter  do
  begin
    read(BookFile, SingleBook);
    BookArray[Count1] := SingleBook;
  end;
closefile (BookFile);

The code I've used to rewrite the file is:
  BookArray[Count].BookAmountAvailable := BookArray[Count].BookAmountAvailable -1;
  assignfile (BookFile, 'BookFile.dat');
  rewrite (BookFile);
  for Count1 := 1 to BookCounter do
    begin
      seek (BookFile, Count1);
      write(BookFile, BookArray[Count1]);
    end;
  closefile (BookFile);

What is it that is causing it to not rewrite the array properly?

A: There are two problems with your code.


*

*Dynamic Arrays are ZERO based, and your indexing them as 1 based arrays.

*Seek() call is not needed.

