Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a circular file in c++

I need to write a circular file in c++. The program has to write lines in a file and when the code reaches a maximum number of lines, it must overwrite the lines in the beginning of the file.

Anyone have any idea?

like image 440
Kram Avatar asked May 20 '09 12:05

Kram


People also ask

How to draw a circle in C programming?

To draw a circle in C programming, first include graphics.h header file in your program. C has given a function to draw a circle, whose prototype is this way... Here, is the center point of the x and y circle. The circle function does not return any value and whatever value you pass in the Circle function is in the pixel form.

What is circular linked list program in C?

Circular Linked List Program in C - Circular Linked List is a variation of Linked list in which the first element points to the last element and the last element points to the first element. Both

How to draw circle with Center at (x) and radius?

The header file graphics.h contains circle() function which draws a circle with center at (x, y) and given radius. Syntax : circle(x, y, radius); where, (x, y) is center of the circle. 'radius' is the Radius of the circle. Examples :

How to draw a rectangle in C graphics?

To draw a rectangle in C graphics, first, you have to initialize the graphics and also include the graphics.h file in your program. Have a look at the Rectangle drawing function prototype below and then we will look forward to how it is used.


2 Answers

that's going to be tricky since file I/O works with bytes as the underlying unit of storage, and not lines.

I mean you could just fseek() back to the beginning and clobber the earlier data, but I have a hunch that's not what you want.

like image 116
Jason S Avatar answered Sep 28 '22 19:09

Jason S


I've seen this done by keeping the current write position for the file somewhere. When you need to add a line, you seek to the position, write the line, and update the position in an atomic fashion. If you overflow, then you seek to zero before you write the line. We do this today for size constrained circular log files. Doing it on a line-constrained basis is a little odd, but could probably be done in a similar fashion. Our write loop looks something like:

logFile.lockForWrite();
currentPosition = logFile.getWritePosition();
logFile.seek(currentPosition);
for each line in lineBuffer {
    if ((currentPosition+line.length()) > logFile.getMaxSize()) {
        currentPosition = 0;
        logFile.seek(0);
    }
    logFile.write(line);
    currentPosition += line.length();
}
logFile.setWritePosition(currentPosition);
logFile.unlock();

The tricky part is in maintaining the current write position and finding some way to coordinate reading the file (e.g., with the tail utility) while your application is writing to it. Your reader utility has to keep track of the write position as well so it's read loop becomes:

lastPosition = logFile.getWritePosition();
while (!killed) {
    logFile.wait();
    logFile.lockForRead();
    newPosition = logFile.getWritePosition();
    logFile.seek(lastPosition);
    newLine = logFile.readFrom(lastPosition, (newPosition-lastPosition));
    lastPosition = newPosition;
    logFile.unlock();
}

This isn't in any particular language - it's just pseudocode but the idea is there. Of course, I left the handling all of the interesting edge cases to the reader.

With all of that said... I agree with the other opinions. Don't do this unless you have a really good reason. It sounds like a great idea, but:

  • the implementation is difficult to get write
  • it's even harder to make efficient
  • since the write position has to be maintained somewhere, multiple utilities have to agree on how it is read, updated, initialized, etc.
  • having a non-linear log makes log processing difficult using existing tools like grep, tail, perl, etc.

Overall, you will be better off using some existing package logging package that allows for configurable log file management. Take a look at Apache's log4cxx or Poco's Poco::Logger.

like image 32
D.Shawley Avatar answered Sep 28 '22 18:09

D.Shawley