in reply to insert zero into binary files ?

Why should anyone help you (lazy)? Didn't you learn anything from your previous questions

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

        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.

      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;