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?
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.
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
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 :
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.
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.
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:
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With