为什么我会收到“错误文件描述符”当我尝试用 Perl 读取文件时?
我试图一次读取 40 个字节的二进制文件,然后检查所有这些字节是否都是 0x00,如果是,则忽略它们。如果没有,它会将它们写回另一个文件(基本上只是删除大块的空字节)。
这可能不是最有效的方法,但我并不担心这一点。但是,现在我收到“错误文件描述符”错误,但我不明白为什么。
my $comp = "\x00" * 40;
my $byte_count = 0;
my $infile = "/home/magicked/image1";
my $outfile = "/home/magicked/image1_short";
open IN, "<$infile";
open OUT, ">$outfile";
binmode IN;
binmode OUT;
my ($buf, $data, $n);
while (read (IN, $buf, 40)) { ### Problem is here ###
$boo = 1;
for ($i = 0; $i < 40; $i++) {
if ($comp[$i] != $buf[$i]) {
$i = 40;
print OUT $buf;
$byte_count += 40;
}
}
}
die "Problems! $!\n" if $!;
close OUT;
close IN;
我在它损坏的地方标记了评论。感谢您的帮助!
I'm trying to read a binary file 40 bytes at a time, then check to see if all those bytes are 0x00, and if so ignore them. If not, it will write them back out to another file (basically just cutting out large blocks of null bytes).
This may not be the most efficient way to do this, but I'm not worried about that. However, right now I'm getting a "Bad File Descriptor" error and I cannot figure out why.
my $comp = "\x00" * 40;
my $byte_count = 0;
my $infile = "/home/magicked/image1";
my $outfile = "/home/magicked/image1_short";
open IN, "<$infile";
open OUT, ">$outfile";
binmode IN;
binmode OUT;
my ($buf, $data, $n);
while (read (IN, $buf, 40)) { ### Problem is here ###
$boo = 1;
for ($i = 0; $i < 40; $i++) {
if ($comp[$i] != $buf[$i]) {
$i = 40;
print OUT $buf;
$byte_count += 40;
}
}
}
die "Problems! $!\n" if $!;
close OUT;
close IN;
I marked with a comment where it is breaking. Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想检查
open
是否不返回错误。You might want to check if
open
doesn't return error.