gmpassos has asked for the wisdom of the Perl Monks concerning the following question:

How can I resize a file without rewrite it?

To grow the size is easy, just write some data to it. But how to reduce the size?! How can I open a file, that already exist, and just cut some data in the end without need to rewrite all of it?! Just imagine that the file can have 500Mb! I really can't rewrite it just to reduce the size.

Graciliano M. P.
"The creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re: Resize a File, without rewrite it!
by zengargoyle (Deacon) on Mar 10, 2003 at 20:17 UTC
    truncate(FILE, $newlen);

    perldoc -f truncate is likely what you want.

      Thanks for the reply! it works. But you need to open your file to append data before:

      open (LOG,">>$file") ; truncate(LOG, 3); close (LOG) ;

      Graciliano M. P.
      "The creativity is the expression of the liberty".

Re: Resize a File, without rewrite it!
by Elian (Parson) on Mar 10, 2003 at 20:18 UTC
    Truncate takes a parameter, which is the new filesize. This will, on at least some operating systems, shrink the file. (This isn't 100% cross-platform, but should work most places)