Reading/Deleting Last Line Of File

Jeff Massung massung at gmail.com
Thu Feb 11 19:27:01 EST 2010


On Thu, Feb 11, 2010 at 5:49 PM, Alex Tweedly <alex at tweedly.net> wrote:

> Jeff Massung wrote:
>
>> Warren,
>>
>> I've read through most of these suggestions, but I'm surprised that the
>> obvious hasn't been suggested yet (that I've seen): skip everything...
>>
>>
>>
> Hasn't been suggested because it won't work.
>
>
>  put the length of url "file:myfile.txt" into tEnd
>> open file "myfile.txt" for text update
>> seek to tEnd in file "myfile.txt"
>>
>> >From here just back up a "reasonable" number of characters... say 200.
>> Find
>> the last CR character, nuke everything else and close the file. Didn't
>> find
>> one? Try backing up another 200, etc. It'll be a whole lot faster.
>>
>>
>>
> "... nuke everything else ...." ?? How ?
>
>
I'm still new to Rev, but in other languages this is dead simple:

void truncate_huge_file(const char* filename)
{
    FILE* fp = fopen(filename,"wb");
    char bytes[200];
    size_t new_len;

    // move to the end of the file, read 200 bytes
    fseek(fp, -200, SEEK_END);
    fread(bytes, 1, 200, fp);

    // .. TODO: scan for end of line, repeat as needed
    // .. TODO: seek SEEK_CUR past what should still be there

    new_len = ftell(fp);

    // nuke everything else at the end of the file
    trunctate(fp, new_len);
    fclose(fp);
}

Done.

Now, maybe this isn't as easy in Rev as it is in C and *many* other
languages. But it should be [if it isn't].

Jeff M.



More information about the use-livecode mailing list