aplonis has asked for the wisdom of the Perl Monks concerning the following question:
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: binmode copy loses final byte
by ikegami (Patriarch) on Jun 30, 2025 at 18:54 UTC | |
by ikegami (Patriarch) on Jun 30, 2025 at 19:19 UTC | |
by aplonis (Pilgrim) on Jul 01, 2025 at 00:46 UTC | |
by ikegami (Patriarch) on Jul 01, 2025 at 12:20 UTC | |
by aplonis (Pilgrim) on Jul 01, 2025 at 13:33 UTC | |
|