如何使用Perl访问文件中的特定字节范围?

noh*_*hat 4 perl file

将磁盘上文件的指定字节范围提取到变量中最方便的方法是什么?

mob*_*mob 5

seek到范围的开头,read所需的字节数(或sysseek/ sysread- 参见nohat的评论).

open $fh, '<', $filename;
seek $fh, $startByte, 0;
$numRead = read $fh, $buffer, $endByte - $startByte; # + 1
&do_something_with($buffer);
Run Code Online (Sandbox Code Playgroud)

  • 为什么需要覆盖`do_something_with()`的原型检查? (4认同)
  • 最后,这不起作用,因为你不能将`seek`调用与`sysread`调用混合.你需要使用`sysseek`代替. (2认同)