We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,344 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Adding records to an existing Typed File

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

2
Contributors
3
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
revski
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Instead of Reset and Seek, you could use Append.

pritaeas
Posting Prodigy
Moderator
9,313 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,465
Skill Endorsements: 86

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
revski
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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) .

pritaeas
Posting Prodigy
Moderator
9,313 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,465
Skill Endorsements: 86

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.3205 seconds using 2.67MB