Reading/Deleting Last Line Of File
jim at visitrieve.com
Tue Feb 9 19:10:50 CST 2010
It does work, just not "exactly" how I said to do it. ;-) You have to open
the file for "append*, instead of *write* and then write at a byte offset.
open file "<filename>" for append
write "This is the EOF" to file "<fileName>" at XXXX
close file "<filename>"
I just tested on an approximately 10kb text file and if XXXX is 5000, the
"This is the EOF" is written starting at byte 5000 and the file is
terminated at the end of that string and the file data before byte 5000 is
retained in the file and the file changes to an approximately a 5KB file, so
the EOF is moved and the file is truncated.
I really shouldn't dash these answers off from memory. ;-)
Aloha from Hawaii,
Alex Tweedly wrote:
> 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
> 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
> >> just remove the last record without reading through the complete
> >> with RunRev?
> > This is totally untested but "logically" you could:
> > 1. First get the size of the file on disk and approximate a character
> > 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
> > 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
> > The reason you don't use open for update is that you want to open the
> > for write, so that whatever you write to the file replaces everything
> > the offset to the EOF. Update will replace chars and leave the last
> > at the end.
> > What I don't know here is if internally the read/write from file at
> > 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
> > doing it this way won't require reading the whole file into memory
> > should result in the last line being deleted from the file in the
> > 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
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
More information about the use-livecode