Reading/Deleting Last Line Of File
Alex Tweedly
alex at tweedly.net
Fri Feb 12 18:26:54 EST 2010
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. ;-)
>
That's not what my testing showed (or appeared to show). Here's the
short version of the test script, and the corresponding output
note the first example has a single final CR, the second has no CR and
the third one has multiple CRs, and each is correctly truncated.
(I missed a few test cases here - empty file, single line of data, etc.
but it is only "reasonably tested")
The test stack can be downloaded from
http://www.tweedly.org/deletelastline.rev
(btw - I don't currently have access to a mac, so it's only tested on
Windows. Could that cause the different result for Richard ?)
> on mouseUp
>
> put empty into field "F"
> put "abcdefg" & CR into t
> put "1234567" & CR after t
> put t into URL ("file:a,b.txt")
> put "abcdefg" & CR into t
> put "1234567" after t
> put t into URL ("file:b.txt")
> put "abcdefg" & CR into t
> put "1234567" & CR &CR after t
> put t into URL ("file:c.txt")
> putfile "a,b.txt"
> deletelastline "a,b.txt"
> putfile "a,b.txt"
>
> putfile "b.txt"
> deletelastline "b.txt"
> putfile "b.txt"
>
> putfile "c.txt"
> deletelastline "c.txt"
> putfile "c.txt"
> end mouseUp
>
> on putfile p
> put "file " && p & CR after field "F"
> put URL ("file:" & P) after field "F"
> put "endoffile" & CR after field "F"
> end putfile
>
giving
> file a,b.txt
> abcdefg
> 1234567
> endoffile
> file a,b.txt
> abcdefg
> endoffile
> file b.txt
> abcdefg
> 1234567endoffile
> file b.txt
> abcdefendoffile
> file c.txt
> abcdefg
> 1234567
>
> endoffile
> file c.txt
> abcdefg
> 1234567
> endoffile
-- Alex.
More information about the use-livecode
mailing list