Reading/Deleting Last Line Of File

Jim Bufalini jim at visitrieve.com
Fri Feb 12 19:59:03 EST 2010


Alex Tweedly wrote:

> I suspect the *trick* of opening a file for append, then seeking back
> into the middle of the file before doing a write is OS-dependent. It's
> certainly not documented (either way), and I was surprised when Jim
> said
> that it could be done.
> Intuitively, 'append' should mean that the existing file is unchangable
> - all you can do is add to it.
> 
> Playing around some more, it appears that the results of write to file
> in these odd cases are not always what you'd expect.
> 
> I would have expected the following two fragments to be equivalent
> 
> > seek to 2 in file "b.txt"
> > write "x" to file "b.txt"
> and
> 
> > write "x" to file "b.txt" at 2
> to be equivalent, but they are not. The latter truncates the file after
> the write, but the former does not (in Windows).

I have never used the *seek* that both you and Richard had in your code
examples for write. I've never even used seek for a read, preferring to keep
track of offsets myself. This is why, in the example that I gave back on the
9th (and tested at the time), I used *open for append - write at offset -
close*. I also have never written an empty, but some truncated data. 

So, I just assumed and it sounded "logical" that seeking and then writing an
empty was the equivalent of what I knew worked. I guess not. Good to know.

In any case, I do know that if you open for append and write at least one
char at an offset, it does truncate the file at the end of that char. And I
know this works on at least PC and Mac.

Aloha from Hawaii,

Jim Bufalini




More information about the use-livecode mailing list