Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Byte ranges of binary files

by zork42 (Monk)
on Jul 29, 2013 at 04:54 UTC ( #1046771=note: print w/ replies, xml ) Need Help??


in reply to Byte ranges of binary files

How about this?

NB untested

use strict; use warnings; use autodie; my $filename = '...some filename...'; my $rangeStart = 10_000; my $rangeEnd = 20_000; open my $fh, "<", $filename; binmode $fh; seek($fh, $rangeStart, 0); my $total_bytes_to_read = $rangeEnd - $rangeStart + 1; my $buffer_size = 1_024; my $buffer; my $num_bytes_to_read; my $num_bytes_actually_read; # read() : Returns the number of characters actually read, 0 at end of + file, or undef if there was an error (in the latter case $! is also +set) while ( ( $num_bytes_to_read = ($buffer_size <= $total_bytes_to_re +ad) ? $buffer_size : $total_bytes_to_read ), # handle partial buffer ( $num_bytes_actually_read = read($fh, $buffer, $num_bytes +_to_read) ), ( (defined $num_bytes_actually_read) && ($num_bytes_actual +ly_read > 0) ) ) { $total_bytes_to_read -= $num_bytes_actually_read; # process $num_bytes_actually_read in $buffer } if ( ! defined $num_bytes_actually_read ) { die $! } # there was an +error. In this example, autodie will detect this, but # + without autodie you'd need to check it yourself. ( $num_bytes_actually_read == 0 ) || die "Should never happen"; # if $total_bytes_to_read > 0, then reached EOF before reading all spe +cified data # ie $rangeEnd > size of $filename


Comment on Re: Byte ranges of binary files
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046771]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (14)
As of 2015-07-08 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (95 votes), past polls