[英]How can I access a specific range of bytes in a file using Perl?
将磁盘上文件的指定字节范围提取到变量中最方便的方法是什么?
seek
to the start of the range, read
the desired number of bytes (or sysseek
/ sysread
-- see nohat's comment). seek
范围的开始, read
所需的字节数(或sysseek
/ sysread
- 请参阅nohat的评论)。
open $fh, '<', $filename;
seek $fh, $startByte, 0;
$numRead = read $fh, $buffer, $endByte - $startByte; # + 1
&do_something_with($buffer);
Sometimes I like to use File::Map , which lazily loads a file into a scalar. 有时我喜欢使用File :: Map ,它会懒惰地将文件加载到标量中。 That turns it into string operations instead of filehandle operations:
这将它变成字符串操作而不是文件句柄操作:
use File::Map 'map_file';
map_file my $map, $filename;
my $range = substr( $map, $start, $length );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.