Having an issue with code below. It's to be for a one-time pad encryption routine (just for fun).

Having an issue with binmode input and output. Losing a byte at the very end of output file. Not sure whether it happens on read or write. Someone please point out my error. TIA

#!user/bin/perl -w my $infile = '/home/me/Documents/Sample.txt'; my $outfile = '/home/me/Documents/Sample_Copy.txt'; open(IN, "< $infile") or die "Can't open $infile: $!"; binmode IN; open(OUT, "> $outfile") or die "Can't open $outfile: $!"; binmode OUT; my $blksize = (stat IN)[11] || 16384; print "\nBlock Size = $blksize\n"; my @bufAry = []; while (my $len = sysread IN, my $buf, $blksize) { if (!defined $len) { next if $! =~ /^Interrupted/; die "System read error: $!\n"; } @bufAry = unpack('I*', $buf); # Disassemble buf into array. print "\nIn = " . scalar @bufAry; # Encryption subs here... undef($buf); # Barrier to buf falling through. # Decryption subs here... $buf = pack('I*', @bufAry); # Reassemble buf from array. print " Out = " . scalar @bufAry; $offset = 0; while ($len) { defined($written = syswrite OUT, $buf, $len, $offset) or die "System write error: $!\n"; last if $written == 0; # Avoid infinite loop. $len -= $written; $offset += $written; } print "\n"; } close(IN); close(OUT);

In reply to 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.