in reply to writing to the top of a file
Apart from that, you might consider looking at the problem a different way: keep the file i/o simple (always append new text at the end of the file, the way God intended), and just change how you read and manipulate the file data for display.
If the people reading the display are only interested in the most recent content, you might decide that they won't want/need to look any farther than the "N" most recent lines. If you have the "tail" utility (everybody should have this by now), why not use it, since it was created to do just what you want (mostly):
If you're offended by the use of backticks, you might consider estimating how many bytes would likely cover $n lines, seek to that many bytes from the end of the file, and then read to the end of the file in the usual way assigning lines to array elements, and reverse the array; the oldest line might be just a fragment, but you could just ignore that one. (And $n could even be user-specified.)my @latest = reverse( `tail -$n $datafile` ); # @latest has the last $n lines from $datafile, last line first...
(update: As usual, BrowserUK has provided a more sensible and effective approach. I'd follow his advice.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: writing to the top of a file
by reTard (Sexton) on May 20, 2004 at 03:55 UTC | |
by graff (Chancellor) on May 20, 2004 at 04:11 UTC |