Efficiently reading the last line from a text file

From the Xojo Forums

DONT use a text input stream

Read it ONCE, keep track of the total size at that time, on the next read, use a binary stream, jump to the last known size, read “the rest”, keep track of this size for next time

Lather rinse & repeat

And if you can avoid that first read of the entire file
Maybe start 4K back from the end and read that
See if there’s more than one line in there
If so you don’t need to jump back any further
Pull “the last line” out of that and save the size as noted above
And now you’re off to the races reading just the last line(s) each time

2 Likes

First time you read the file read it from end backwards until you hit a new line, there’s your last line, then as above keep a pointer to the end of each line so you can read the next.