Need efficient way to read file in reverse

Rahul Dhesi dhesi%cirrusl at oliveb.ATC.olivetti.com
Wed Sep 26 13:43:07 AEST 1990


The question from Roy Smith was:

     Is there a standard, portable, efficient way to read a file in
     reverse?

The word "portable" is the catch here.  There is *no* truly portable
way to seek to a place in a file where you haven't been before.  Worse,
there is *no* truly portable way to seek to *any* place in a text file
whether or not you have been there before.  (I'm sure some people will
protest at the second claim and mention magic cookies, but they may be
assuming that "portable" means "ANSI-conformant".  This is not a
portable assumption :-)

Since this is a comp.unix.* discussion, however, we may be able to
modify the meaning of "portable" somewhat by restricting ourselves to
solutions that work in a **IX environment.

In <11217 at celit.fps.com> hutch at fps.com (Jim Hutchison) writes:

>The ideal block size for reading your file can be gotten by doing a stat() or
>fstat() on it, and using stat->st_blksize (see stat(2)).  If you setbuffer()
>with a buffer that size, you should have a quasi-optimal block size.

Alas, neither stat->st_blksize nor setbuffer() are universally
available, not even in a **IX environment.  You may need to use
setvbuf() instead, which itself isn't universally available.

Modifying the meaning of "portable" still further, perhaps we could
settle for it to mean "works under 4.3BSD and System V Release 4".  Now
you're talking!
--
Rahul Dhesi <dhesi%cirrusl at oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi



More information about the Comp.unix.wizards mailing list