in reply to Re: insert zero into binary files ?
in thread insert zero into binary files ?

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 ?

Replies are listed 'Best First'.
Re: Re: Re: insert zero into binary files ?
by castaway (Parson) on Nov 06, 2003 at 08:29 UTC
    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.

      Castway, Thanks a lot ....

      you are very very powerfull and your ideas is very very good. I'm really respect on that...

      3dan, thanks ....
      I have solved my problem by referring at your example and castway guide.

      bh.

Re: Re: Re: insert zero into binary files ?
by edan (Curate) on Nov 06, 2003 at 08:29 UTC

    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

      I would suggest a slight modification

      my $desired_size = 2048000; my $current_size = -s $file; my $null_bytes = "\x00" x ($desired_size - $current_size); open OUT, ">>", $file or die; binmode OUT; # <-- Some OSs need this print OUT $null_bytes; close OUT;