max file size

I have a log file generated by my program, one of the options is a maximum size for the log file. What I need to be able to do is append my new log entry then check the file size. If the file size is too large remove a chunk of it.

I know that I CAN simply make a temp file write only the info I want there, then erase the original file and replace it with the temp. but I don't want to do that.

How can I remove X amount of data from the start of the file without creating another file?
LucidityAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ufolk123Commented:
Hi Lucidity,


It is not possible to shrink a file of size x to some size y other than zero.
I can understand your problem.I had faced the same problem in one of my projects.

If I understood correctly you want to do something like

For Each LOG write opertion

Check the size of log file
If size < MAX_LOG_SIZE then write to end
else
overwrtie in such a way that new log is written from starting position.

For this you need to do following



Use the  2 bytes of file will always be the current log position.
During create a new log file this  position=0;

for each log write
Read the log position ( first two bytes).
if log position<MAX_LOG_SIZE
    lseek to offset (log position)
    fprintf log
    update the log postion to new value
else
    fprintf log
    update the log postion 3+fprintf string
endif

Regards,
ufolk123

   


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
>> It is not possible to shrink a file of size x to
>> some size y other than zero.
It is true that in standard C++ it is not possible to do this.  However most operating systems do have a feature to do this.  

For example, I beleive Lucidity woks in windows, so he can use SetEndOfFile()  (and GetFileSize()).   Of course this makes the code non-portable.
0
LucidityAuthor Commented:
SetEndOfFile could work except the newest entries are being appending to the end so I would be cutting off new entries. That won't work unless there is a way to append to the front of a file :) thats another question all together.
0
nietodCommented:
I didn't realize you wanted to save the new entries and remove the older ones.

For that, you can write to the file as if it were a circular queue.  i.e when you reach the end, start storing entries at the beginning again.  This will always keep the most recent entries in the file, but they won't be in chronolgical order.  If they have to be in order, you will have to shift the entries forward by reading them and wrting them in an earlier position.
0
LucidityAuthor Commented:
well.. thats exactly the same answer I already have :), I will be implementing it later this week.

Jason
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.