in reply to how to give size to a file at the time of creation

Not sure why you would want to start off with a big file but here you go, This makes a big file full of zeros. You could also use pack to get a null and fill it with those if you prefer. Or question marks as the requirement is puzzling :)

use strict; use warnings; my $file = "test.meg"; my $mb = 1024 * 1024; my $meg = '0' x $mb; open my $fh, '>', $file or die "yuck, $file: $!\n"; print $fh $meg; seek $fh, 0, 0; # rewind to print at start. I guess you want this print $fh "I Write\n"; close $fh;

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: how to give size to a file at the time of creation
by Tux (Canon) on May 03, 2013 at 08:00 UTC

    If your filesystem supports sparse files, you don't need to write it all, one byte will do:

    $ rm -f xx ; perl -wE'open$a,">","xx";seek$a,1048575,0;print$a "x"' ; +ls -l xx -rw-rw-rw- 1 merijn users 1048576 May 3 09:59 xx

    Enjoy, Have FUN! H.Merijn

      I tried seek first, but I am on a work XP box and it just gave me a small file. Like you said if your OS supports...

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!