Reading/Deleting Last Line Of File

Alex Tweedly alex at tweedly.net
Tue Feb 9 18:52:38 EST 2010


I don't think this will work, Jim.  If you open a file for write, then 
it erases the entire content of the file first

as the docs say (emphasis added)...
> The file to write to must be opened first with the open file command, 
> and the mode the file was opened in must be write, append, or update. 
> If the file is not open or is open read-only, the result function is 
> set to "File is not open for write.".
>
> If the file was opened in write mode, the write to file command 
> completely *replaces the file contents from the start.* For example, 
> if the file originally contains "ABC", and you write "1" to it, after 
> the write to file command is executed the file contains "1".
And as you say, open for update does not allow you to truncate the file.

The standard C library has a way to do it, and there is a Unix shell 
command - but not afaict a Windows shell utility to do it.

-- Alex



Jim Bufalini wrote:
> 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
>
>
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
>
>   




More information about the use-livecode mailing list