Reading/Deleting Last Line Of File

Jim Bufalini jim at visitrieve.com
Tue Feb 9 09:34:16 EST 2010


Warren,

> I have a large text file (100,000,000+ records).  I need to write a
> utility that removes the last record of the file.  Is there anyway to
> just remove the last record without reading through the complete file
> with RunRev?

This is totally untested but "logically" you could:
 
1. First get the size of the file on disk and approximate a character offset
that should be only a few lines from the end of the file. 

2. Then Open the file for read and read from file starting at the char
offset you calculated to EOF into a var and delete the last line of the var.


3. Then Close the file and Open it again for write.

4. Now write the var to file at the same starting offset you read from.

The reason you don't use open for update is that you want to open the file
for write, so that whatever you write to the file replaces everything from
the offset to the EOF. Update will replace chars and leave the last record
at the end.

What I don't know here is if internally the read/write from file at start
still has to go through the entire file to calculate the starting offset, so
I don't know how long this will take on a file of your size. But, at least,
doing it this way won't require reading the whole file into memory and
should result in the last line being deleted from the file in the quickest
way available.

Aloha from Hawaii,

Jim Bufalini






More information about the use-livecode mailing list