hi

i seem to have run into a problem when adding records to a Typed file that already exists.

I have used ReWrite method to create the file in the first place.

// Open the file for writing To
      AssignFile(F, ExtractFilePath(Application.ExeName) + 'bin\drivers\' +
        (frm_vars.Driver1.Caption) + '\card.digi');
      Rewrite(F);
      // Move To The End Of The File
      Seek(F, FileSize(F));
      // Write a couple of records to the file
      Limits.tenhourdrive := ShortString(frm_vars.L10HrDriveAvailable1.Caption);
      Limits.fifteenhourshift :=
        ShortString(frm_vars.L15HrShiftAvailable1.Caption);
      Write(F, Limits);
      // Close the file
      CloseFile(F);

however if i check if the file exists, and then add records using Reset(F); it duplicates the first records in the file and then adds new ones on the end.

Can somebody who knows more than me please explain why this might be happening.

Here is the code im using to add records to an existing file

if SysUtils.FileExists(LimitsFile) then
    begin
      AssignFile(F, ExtractFilePath(Application.ExeName) + 'bin\drivers\' +
        (frm_vars.Driver1.Caption) + '\card.digi');
      Reset(F);
      // Move To The End Of The File
      Seek(F, FileSize(F));
      // Write a couple of records to the file
      Limits.tenhourdrive := ShortString(frm_vars.L10HrDriveAvailable1.Caption);
      Limits.fifteenhourshift :=
        ShortString(frm_vars.L15HrShiftAvailable1.Caption);
      Write(F, Limits);
      // Close the file
      CloseFile(F);
      ShowMessage('File Updated!');
      Reset(F);
    end;

thanks

Recommended Answers

All 3 Replies

Instead of Reset and Seek, you could use Append.

I have tried using append like you suggested, but when trying to compile i get an error:

"E2008 Incompatible Types"

if SysUtils.FileExists(LimitsFile) then
    begin
      AssignFile(F, ExtractFilePath(Application.ExeName) + 'bin\drivers\' +
        (frm_vars.Driver1.Caption) + '\card.digi');
      Append(F);
      // Write a couple of records to the file
      Limits.tenhourdrive := ShortString(frm_vars.L10HrDriveAvailable1.Caption);
      Limits.fifteenhourshift :=
        ShortString(frm_vars.L15HrShiftAvailable1.Caption);
      Write(F, Limits);
      // Close the file
      CloseFile(F);
      ShowMessage('File Updated!');
      Reset(F);
    end;

i have been searching on the internet, but i cant find anything about using append on typed files, when using

var
F: File Of TLimits

My bad, my Pascal is somewhat rusty.

If you want to use Seek, see this link. You should not pass the FileSize, but the number of records it represents, so probably FileSize(F) div SizeOf(TLimits) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.