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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |