troubleshooting Question

Append to a Binary File

Avatar of falthorn
falthorn asked on
DelphiPascal
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.

[code]

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

[/code]

Can you please advise.
ASKER CERTIFIED SOLUTION
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