Re: FIFO File writing
From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 08/26/04
- Next message: Peter Ammon: "Re: Getting rid of int"
- Previous message: TLOlczyk: "Re: Need advice."
- In reply to: Kirsten: "FIFO File writing"
- Next in thread: Kirsten: "Re: FIFO File writing"
- Reply: Kirsten: "Re: FIFO File writing"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 26 Aug 2004 03:25:39 GMT
Kirsten wrote:
>
> This night I searched the whole internet... and I cannot believe
> that there is no possibility the write to a file "first in -
> first out"? Well, how to implement a file logging system? How to
> implement history file writer?
>
> I want to add a new line at the end of my log file, after that
> the first line should deleted, so my file cannot get bigger and
> bigger. Working in memory this data structure would be called
> queue. But I want/need it for files (immediate physical saving).
First, fix your line length. I had to reformat the above. Your
lines should not exceed 78 chars ever, and 65 is much better.
Create a file of fixed length records. Reserve the zeroth record
to show the current write pointer, which will always be pointing
at the oldest record. To read the file, read record 0, seek the
appropriate record, read from there to the end, and from record 1
up to the initial record.
To write a new record, read the 0th, form the pointer, seek there
and write the new record. Then seek the 0th and write back
whatever you represent the current pointer (incremented) by.
The easiest way would be to make all those records simply strings
of ASCII chars. But you will want to put them in and out with
binary operations, i.e. this is a binary file.
The file is vulnerable between writing the new record and writing
the 0th index record, so you probably need to open it with
exclusive write access. You might be able to inhibit any readers
by first writing an invalid marker to the index record, but watch
out for recovery from crashes.
Figure out the initialization for yourself.
-- Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address!
- Next message: Peter Ammon: "Re: Getting rid of int"
- Previous message: TLOlczyk: "Re: Need advice."
- In reply to: Kirsten: "FIFO File writing"
- Next in thread: Kirsten: "Re: FIFO File writing"
- Reply: Kirsten: "Re: FIFO File writing"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|