Link to home
Start Free TrialLog in
Avatar of Lucidity
Lucidity

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of ufolk123
ufolk123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

>> 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.
Avatar of Lucidity

ASKER

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.
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.
well.. thats exactly the same answer I already have :), I will be implementing it later this week.

Jason