简体   繁体   English

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

[英]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.

相关问题 如何使用Perl在文件中找到特定的行? - How can i find a specific line in a file using Perl? 如何使用Perl将48十六进制字符串转换为字节? - How can I convert a 48 hex string to bytes using Perl? 我如何使用perl中的原始字节 - How can I work with raw bytes in perl 如何在不使用Perl中的正则表达式的情况下在文件中找到特定行? - How can I find a specific line in a file without using regular expressions in Perl? 使用Perl,如何从日志文件中仅提取特定分钟内发生的错误? - Using Perl, how can I extract from a log file only the errors that happened during a specific minute? 如何使用Perl的HTML :: TableExtract从HTML文件中提取带有标题名称的特定列 - How can I extract specific columns with header names from an HTML file using Perl's HTML::TableExtract 如何在Perl中将文件的路径更正为特定于OS的文件? - How can I correct the path of a file to be OS specific in Perl? 如何使用perl搜索xml文件中的特定字符串? - How can I search an xml file with perl for a specific character string? 如何使用 perl 删除变量值的特定部分 - How can i remove a specific part of a variable value using perl 使用Perl,如何查找和删除具有特定后缀的文件? - Using Perl, how can I find and remove files with a specific suffix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM