#!/usr/bin/perl use strict; use warnings; my $in_qfn = '...'; my $out_qfn = '...'; open( my $in_fh, "<:raw", $in_qfn ) or die( "Can't open `$in_qfn`: $!\n" ); open( my $out_fh, ">:raw", $out_qfn ) or die( "Can't create`$out_qfn `: $!\n" ); my $int_size = 4; # length( pack( "L<", 0 ) ); local $/ = \( 8 * 1024 ); while ( my $in_buf = <$in_fh> ) { length( $in_buf ) % $int_size == 0 or die( "Invalid input file `$in_qfn`\n" ); my @ints = unpack( "L<*", $in_buf ); # Something that modifies `@ints` here. print( $out_fh pack( "L<*", @ints ) ) or die( "Error writing to `$out_qfn`: $!\n" ); } close( $in_fh ) or die( "Error reading from `$in_qfn`: $!\n" ); close( $out_fh ) or die( "Error writing to `$out_qfn`: $!\n" );

Without readline (<>) and print:

#!/usr/bin/perl use strict; use warnings; my $in_qfn = '...'; my $out_qfn = '...'; open( my $in_fh, "<:raw", $in_qfn ) or die( "Can't open `$in_qfn`: $!\n" ); open( my $out_fh, ">:raw", $out_qfn ) or die( "Can't create`$out_qfn `: $!\n" ); my $blk_size = ( stat( $in_fh ) )[ 11 ] || 16384; my $int_size = 4; # length( pack( "L<", 0 ) ); $blk_size % $int_size == 0 or die( "Invalid block size\n" ); my $in_buf = ''; while ( 1 ) { my $bytes_read = sysread( $in_fh, $in_buf, $blk_size, length( $in_b +uf ) ); defined( $bytes_read ) or die( "Error reading from `$in_qfn`: $!\n" ) ; $bytes_read or last; my @ints = unpack( "L<*", $in_buf ); substr( $in_buf, 0, @ints * $int_size, "" ); # Something that modifies `@ints` here. { my $out_buf = pack( "L<*", @ints ); my $total_to_write = length( $out_buf ); my $total_written = 0; while ( $total_to_write ) { my $bytes_written = syswrite( $out_fh, $out_buf, $blk_size, $ +total_written ); defined( $bytes_written ) or die( "Error writing to `$out_qfn`: $!\n" ); $total_written += $bytes_written; $total_to_write -= $bytes_written; } } } length( $in_buf ) == 0 or die( "Invalid input file `$in_qfn`\n" ); close( $in_fh ) or die( "Error reading from `$in_qfn`: $!\n" ); close( $out_fh ) or die( "Error writing to `$out_qfn`: $!\n" );

In reply to Re^2: binmode copy loses final byte by ikegami
in thread binmode copy loses final byte by aplonis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.