troubleshooting Question

Append to a Binary File

Avatar of falthorn
falthorn asked on
3 Comments1 Solution1712 ViewsLast Modified:
How do you append to a binary file?

For example, if I use the code that is listed below, the file is recreated every time it is run.  Replacing the ReWrite(myFile) command with Append(myFile), I get an error with binary files, but it works with Text only files.


   TCustomer = Record
     name : string[20];
     age  : Integer;
     male : Boolean;
   myFile   : File of TCustomer;  // A file of customer records
   customer : TCustomer;          // A customer record variable
   // Try to open the Test.cus binary file for writing to
   AssignFile(myFile, 'Test.cus');
   // Write a couple of customer records to the file := 'Fred Bloggs';
   customer.age  := 21;
   customer.male := true;
   Write(myFile, customer); := 'Jane Turner';
   customer.age  := 45;
   customer.male := false;
   Write(myFile, customer);
   // Close the file
   // Reopen the file in read only mode
   FileMode := fmOpenRead;
   // Display the file contents
   while not Eof(myFile) do
     Read(myFile, customer);
     if customer.male
     then ShowMessage('Man with name '
                      ' is '+IntToStr(customer.age))
     else ShowMessage('Lady with name '
                      ' is '+IntToStr(customer.age));
   // Close the file for the last time


Can you please advise.
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros