Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: truncate a file from top

by periapt (Hermit)
on Nov 02, 2004 at 13:15 UTC ( #404583=note: print w/replies, xml ) Need Help??

in reply to truncate a file from top

I assume that you want to preserve the older entries and just overwrite existing ones. The relevent question in this case is whether each line or log block is of fixed size.

For example, if you structure your log messages to be, say, 384 bytes or less (any size will do), then open your log in r/w mode. If the file is of the correct size, position the pointer at the beginning of the file and begin writing your standard blocks. If the file is not yet of the correct size, position the file pointer at the end of the file and keep adding.

The only thing you need is a MAXSZ, CURRSZ and BLOCKSZ variable to track location.

I'm sure there are other ways but this was the first one that came to mind.

use strict; use warnings; use diagnostics;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://404583]
[oiskuu]: Someone needs to re-read How do I use the power of consideration responsibly? carefully. Consideration is not intended for stamping out humor, AFAIK.
[oiskuu]: E.g. Re^3: Big thank you to the Perl community. is surely dry humor (not mine). Don't they teach humor in german schools!?
[LanX]: you don't see the whole picture
[LanX]: But reu germans have no humor, better to refrain from this site till the troll is gone...
[LanX]: true

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2017-04-27 19:51 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (512 votes). Check out past polls.