Reading/Deleting Last Line Of File

Alex Tweedly alex at tweedly.net
Fri Feb 12 18:25:06 CST 2010


Richard Gaskin wrote:
> Jim Bufalini wrote:
>
>> Richard Gaskin wrote:
>>
>>> But FWIW, I tried your version and it seemed to leave the file
>>> unchanged
>>
>> If your original file ends in a cr then Alex's code would end in 
>> absolutely
>> no change. ;-)
>
> Thanks, Jim.  I tried it both ways; no change to the file's contents, 
> although the mod date gets updated so I know it's trying.
I strongly suspect it only works on Windows :-(   Sorry.
I don't have either a Mac or Linux box to try it on, however I did just 
try it on the on-rev server (as an .irev script) and it also appears to 
leave the file unchanged there.

In fact, it gives an error ("error in offset expression") on the seek, 
because the test file is less than 1000 chars - an error which didn't 
get flagged up on Windows. I changed the line from
    seek tFileLength-K in file pFile
to
   if tFileLength-k > 0 then seek tFileLength-K in file pFile
to avoid the error. But it then leaves the file unchanged.

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).

Could you try this in your multi-line msg box and let me know what it 
does on Mac ?
> put "abcdefg" & CR into URL "file:b.txt"
> put URL "file:b.txt"
> open file "b.txt" for append
> write "x" to file "b.txt"  at 2
> close file "b.txt"
> put URL "file:b.txt" after msg
on Win, it produces
> abcdefg
> abx
Thanks
-- Alex.










More information about the use-livecode mailing list