The Windows file system doesn't support "inserting" data, so you have to either 
write to the end of the file, or do what you've done and re-write the file with 
new data at the front.

If you are able to provide a reader for the log file, you could provide one 
that shows the "sections" of the log (using whatever convention, e.g. two cr/lf 
sequences in a row marks a new section and all writes end with that sequence) 
in reverse order and just append data to the end.  Or, if you're writing to an 
NTFS drive, you could perhaps take advantage of the fact that you can have a 
"sparse file" -- one that doesn't take disk space for sections not explicitly 
written.  The first 4 bytes of the file could store the first in-use byte, and 
when you write you keep that updated.  Your reader program would need to deal 
with that, of course.

Or, you could make multiple log files named by the date/time of the file 
(perhaps a new one every hour, or if it's very active, every N minutes) and 
suggest that users look at the files via an Explorer view that sorts them in 
descending name order.

At 07:22 AM 11/18/2005, Girish Jain wrote
>Hi All,
>
>I am writing a log file with the requirement that the latest entry in the
>log should be on the top. For the purpose I looked for ways of doing that
>but ended up by reading the entire contents of the existing file into a byte
>array and then re-write the file with the new content. Is there any better
>way possible?


J. Merrill / Analytical Software Corp

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to