954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read the LAST 14 Entries made in a Typed File

Hi

i need to read the last 14 Entries made into a typed file and then list the entries in a StringGrid so i can copy them to label captions later.

I am a little unsure how to write the Code to loop for the last 14 Entries.

the current code i have is this

if SysUtils.FileExists(ShiftFile) then
  begin
    // Reopen the file in read only mode
    AssignFile(F, ShiftFile);
    Reset(F);
    try
      // Display the file contents
    while Not Eof(F) do
      Read(F, shift);
      // Here i need to find the last 14 Entries
    finally
      CloseFile(F);
    end;


i know that the While Not EoF will just read the entire file until it reaches the end.

How could i make it Read only the last 14 Entries and then list each one in a StringGrid so i can use them later.

thank you for your time.

redrobby02
Newbie Poster
6 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

I'll just give you a few more commands to play with :-)

On a typed file, you just use the command FileSize(Type).
It gives you the exact number of records in the file.
:icon_exclaim: just remember that 0 is also a record.

Now for the coding:

VAR
  TheStart, TheEnd:INTEGER;
BEGIN
.... ... ..
  TheEnd:=FileSize(YourAssignedFileType);
  TheStart:=FileSize-14;
  DEC(TheStart); // We need to decrease this value by one because we count Zero too.
  Seek(YourAssignedFileType,TheStart);// Jumps to the correct location of data.
  REPEAT
    .... read the data
    INC(TheStart);
  UNTIL TheStart=TheEnd;
.. ... ....
END;


I am sure you figure the rest out.
If you have less than 14 records, you need to make some code to avoid error there too.

Best regards.
Morten, Norway.

Morten Brendefu
Light Poster
44 posts since Mar 2011
Reputation Points: 38
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: