"Mark" <ma*************@excite.com> wrote:
# Hi, I have an app that needs to maintain a log file of certain events that
# occur. Normally FILE I/O appends new text to the end of a file. However
# one of the requirements of my app is that the most recent log entries need
# to be at the beginning of the file. My initial thought is that the file
# would have to be rewritten every time a new log entry is added. Can anyone
# suggest a better/simpler way?
Most operating systems you're likely to use today don't support anything
but a long string of bytes, and so neither will most C implementations. There
are many packages written in ANSI C that you can get, or write your own;
for example you can use something like Berkely DB and then write with
decreasing keys so that logical organisation is most recent first. However
such files are unlikely to be usable with most other programs.
# An alternative would be to write the log as normal, then use a separate app
# to post-process the log file into the most-recent-first order required.
On systems like unix, if you write the file as character lines beginning with
a timestamp, you can easily rearrange the file order with 'sort -r'. And unless
your files are huge (like hundreds of megabytes), the total wall clock and
cpu time used by a simple minded approach will actually be less than trying
to be clever
--
Derk Gwen
http://derkgwen.250free.com/html/index.html
GERBILS
GERBILS
GERBILS