[fpc-pascal] read lines at end of file

Mattias Gaertner nc-gaertnma at netcologne.de
Mon Apr 11 19:55:01 CEST 2011


On Mon, 11 Apr 2011 19:45:10 +0200
José Mejuto <joshyfun at gmail.com> wrote:

> Hello FPC-Pascal,
> 
> Monday, April 11, 2011, 6:32:20 PM, you wrote:
> 
> JL> Need to read the last 4 lines (txt) of a very large log file 100M bytes,
> JL> 100k lines or more  lots of times - of course I can read through the file in
> JL> the normal way but this is rather slow. Is there a way to 'read lines from
> JL> the end'? I note that unix tail or its port to windows does this very fast
> JL> so guess there must be a fast way to do it in pascal too?
> 
> The idea is easy, instead reading forward, you read backwards in
> blocks of, say, 64 Kb and count lines back. If not enougth lines you
> insert another 64 Kb before and so on. Average line is usually less
> than 100 bytes, so each 64 Kb are usually more than 600 lines. Read of
> 64 Kb usually takes the same time as read any lower amount when the
> block is 4096 bytes aligned.

You can not really read backwards. You can use
something like TFileStream.Seek(-4096,soEnd).


Mattias



More information about the fpc-pascal mailing list