Reading/Deleting Last Line Of File
massung at gmail.com
Thu Feb 11 18:27:01 CST 2010
On Thu, Feb 11, 2010 at 5:49 PM, Alex Tweedly <alex at tweedly.net> wrote:
> Jeff Massung wrote:
>> 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.
>> the last CR character, nuke everything else and close the file. Didn't
>> 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");
// 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
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].
More information about the use-livecode