Inserting into a stream

Z

Zenon

I have been tasked with writing code which overrides the TraceListener
class for the purpose of limiting the size of the log file. Basically,
when the log file grows to a certain size, I need to begin overwriting
it from the beginning. I am using a ReadWrite Stream to do this. My
problem is the fact that the lines which are to overwrite the old ones
are not necessarily going to be the same size. For instance, if the
first line in the log file was 20 bytes and then a new line character,
the line to overwrite it might be 26 characters before its new line.
How can I insert this without overwriting part of the next line in the
stream. Is there any way to "insert" into the stream?

thanks,

Z
 
G

Guest

Zenon,
There are a number of ways to approach this, including the use of "Rollover"
log files where the original file is given a new name and a new current log
file is started.

But if you only want to do what you state in your post, all you probably
need to do is first check the size of the file. If it is not over the limit
you would open the file for writing with FileMode.Append, otherwise you would
use FileMode.Create and this would automatically overwrite the old file,
starting with a brand new file of the same name.

Keep it simple.
Peter
 
Z

Zenon

Sorry, I probably didn't explain this well. I am actually supposed to
implement 2 different rollover methods. The one you described about
starting a new file when the max size is reached is already done, that
was the easy one. The second method I attempted to describe only
creates one file. When that file reaches a certain size, it begins
overwriting itself, one line at a time so that the maximum amount of
history is retained. For instance, if the max file size is reached
after 10 lines are written, the 11th would overwrite the first but 2
throuth 10 would remain intact.
 
C

Carl Daniel [VC++ MVP]

Zenon said:
Sorry, I probably didn't explain this well. I am actually supposed to
implement 2 different rollover methods. The one you described about
starting a new file when the max size is reached is already done, that
was the easy one. The second method I attempted to describe only
creates one file. When that file reaches a certain size, it begins
overwriting itself, one line at a time so that the maximum amount of
history is retained. For instance, if the max file size is reached
after 10 lines are written, the 11th would overwrite the first but 2
throuth 10 would remain intact.

This second scenario is not realistic - it requires that you re-write all of
the file contents from the "insertion point" to the end every time you write
a message.

The only practical way to achieve something like that is to use a log file
with fixed-length records - which pretty much rules out text files.

-cd
 
Z

Zenon

Fair enough, thanks.
This second scenario is not realistic - it requires that you re-write all of
the file contents from the "insertion point" to the end every time you write
a message.

The only practical way to achieve something like that is to use a log file
with fixed-length records - which pretty much rules out text files.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top