[Tutor] File Access

R. Alan Monroe amonroe at columbus.rr.com
Mon Apr 5 17:53:53 EDT 2004


> thanks for the info. However, f.seek(0,2) takes me to the end of the
> file as you say, but what I need is the whole last line of the file.

> After doing f.seek(0,2) successive f.seek(-1, 1) calls will walk me
> backwards through the file but to make this useful I would need to keep
> checking the current character for an "\n". And unfortunately calls to
> f.read() and f.readline() take me back to the end of the file or line
> respectively.

> Im on linux and the fseek(3) manpage seems to indicate that I cannot
> take a file of unknown size and seek to the start of the last line,
> would be nice if it did tho ;)

If you were desperate you could look at the source code for gnu's
version of the good old tail command.

One thing you could try is reading successively larger and larger
chunks from the end of the file, like powers of two (1 byte, 2 bytes,
4 bytes, 8 bytes, 16 bytes, etc.) until you grab a chunk that has a \n
in it, then work forward from there. Haven't personally tried this,
though.


Alan




More information about the Tutor mailing list