3

我正在尝试使用以下代码读取二进制文件:

open(F, "<$file") || die "Can't read $file: $!\n";
binmode(F);
$data = <F>;
close F;

open (D,">debug.txt");
binmode(D);
print D $data;
close D;

输入文件为16M;debug.txt 只有大约 400k。当我在 emacs 中查看 debug.txt 时,最后两个字符是 ^A^C(根据 notepad++ 的 SOH 和 ETX 字符),尽管 debug.txt 中存在相同的模式。文件中的下一行确实有一个 ^O (SI) 字符,我认为这是该特定字符的第一次出现。

如何阅读整个文件?

4

3 回答 3

5

如果您真的想一次读取整个文件,请使用 slurp 模式。$/通过将(输入记录分隔符)设置为 可以打开 Slurp 模式undef。这最好在一个单独的块中完成,这样您就不会弄乱$/其他代码。

my $data;
{
    open my $input_handle, '<', $file or die "Cannot open $file for reading: $!\n";
    binmode $input_handle;
    local $/;
    $data = <$input_handle>;
    close $input_handle;
}

open $output_handle, '>', 'debug.txt' or die "Cannot open debug.txt for writing: $!\n";
binmode $output_handle;
print {$output_handle} $data;
close $output_handle;

用于my $data词法和our $data全局变量。

于 2010-08-17T13:33:38.227 回答
3

蒂姆托迪

File::Slurp是表达您想要实现的目标的最短方式。它还具有内置的错误检查功能。

use File::Slurp qw(read_file write_file);
my $data = read_file($file, binmode => ':raw');
write_file('debug.txt', {binmode => ':raw'}, $data);

IO::FileAPI$/以更优雅的方式解决了全局变量问题。

use IO::File qw();
my $data;
{
    my $input_handle = IO::File->new($file, 'r') or die "could not open $file for reading: $!";
    $input_handle->binmode;
    $input_handle->input_record_separator(undef);
    $data = $input_handle->getline;
}
{
    my $output_handle = IO::File->new('debug.txt', 'w') or die "could not open debug.txt for writing: $!";
    $output_handle->binmode;
    $output_handle->print($data);
}
于 2010-08-17T14:05:19.053 回答
0

我不认为这与是否使用 slurp 模式有关,而是与正确处理二进制文件有关。

代替

$data = <F>;

你应该做

read(F, $buffer, 1024);

这只会读取 1024 字节,因此您必须增加缓冲区或使用循环逐部分读取整个文件。

于 2013-10-31T15:40:18.890 回答