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

Hi...

Could somebody help me how to append zero values into binary files.

Thank you,
bh_perl

Replies are listed 'Best First'.
Re: insert zero into binary files ?
by Anonymous Monk on Nov 06, 2003 at 07:55 UTC
      Thank for your advices,

      But, previously I'm dealing with ASCII files and now binary files. There should be some different here and I did not see any help on my previous cases.

      Ok, This is my program :

      #!/usr/bin/perl -w undef $/; my $file = <>; my $i = length($file); my $zero = (2048000 - $i) x chr(0); print "$file" . "$zero";
      and the error is :
      Argument "" isn't numeric in repeat (x) at ./a.pl line 6, <> chunk 1.

      Could some body help me ?
        The error message tells you exactly what is wrong. Look up the 'x' operator in the perlop manpage. You have the arguments the wrong way around, it should be $str x $number.

        And if you're trying to write to the file you're reading from, that's not how to do it, you'll need to open the file read/write, using '+>' in open. The syntax to print to a filehandle is:  print $file $zero (Yours tries to print the value of the filehandle and $zero to the default filehandle, which is probably STDOUT (the screen).

        C.

        Hi. I'm really not sure what you want to do, but my best guess is that you have a file, and you want to pad it to a certain length (2048000) with null (zero) bytes. If that's the case, try this totally untested code as a starting point:

        my $desired_size = 2048000; my $current_size = -s $file; my $null_bytes = "\x00" x ($desired_size - $current_size); open OUT, ">>", $file or die; print OUT $null_bytes; close OUT;
        --
        3dan