Re: FIFO File writing

From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 08/26/04


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!


Relevant Pages

  • Re: FIFO File writing
    ... > To write a new record, read the 0th, form the pointer, seek there ... > binary operations, i.e. this is a binary file. ... > The file is vulnerable between writing the new record and writing ... > by first writing an invalid marker to the index record, ...
    (comp.programming)
  • Re: Memory management and allocation
    ... > As I'm writing a piece of code that basically acts as a server and ... > memory management is a topic that is quite crucial. ... Or can I just allocate the variable ... Nor is it usually necessary to set the pointer to ...
    (comp.lang.c)
  • Re: gdb not catching out-of-bounds pointer
    ... is also not defined by the C-standard, IOW: writing code in anything ... provided the library writer knows what the compiler writer guarantees ... etc.) that don't point into the same array than I would about the sort ... of pointer aliasing issue that started this sub-thread. ...
    (comp.unix.programmer)
  • Re: Answer needed
    ... as the last element of the array the data was written outside the allotted ... I re-wrote the offending function with proper checks of the pointer value. ... > including what you expect writing code to see what the result will be ...
    (alt.comp.lang.learn.c-cpp)