[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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?
0
Lucidity
Asked:
Lucidity
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now