为什么我会收到“错误文件描述符”当我尝试用 Perl 读取文件时?

发布于 2024-08-30 23:53:43 字数 691 浏览 2 评论 0原文

我试图一次读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩动你心 2024-09-06 23:53:43

您可能想检查 open 是否不返回错误。

open IN, "<$infile" or die "Can't open $infile: $!";

You might want to check if open doesn't return error.

open IN, "<$infile" or die "Can't open $infile: $!";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文