Chinaunix

标题: 请问文件句柄“关闭”的问题! [打印本页]

作者: tempo8    时间: 2012-05-19 22:41
标题: 请问文件句柄“关闭”的问题!
本帖最后由 tempo8 于 2012-05-19 22:44 编辑

请问文件句柄“关闭”的问题!
源于 http://bbs.chinaunix.net/thread-3744895-1-1.html

  1. my @content;
  2. my $word;
  3. my %hash_file2;
  4. my $i = 1;
  5. my $count;
  6. open FD1, "<file1";
  7. open FD2, "<file2";
  8. while(<FD2>) {
  9.      chomp;
  10.      $hash_file2{$i} = $_;
  11.      $i++;
  12. }
  13. while ($word = <FD1>) {
  14.     chomp($word);
  15.     $count = grep {/\b$word\b/}  values %hash_file2;
  16.     push @content, $word if $count > 0;
  17. }
  18. print @content;
复制代码
C:\strawberry\perl\bin>perl word-map.pl <A.txt <B.txt >111.txt
运行时出现问题:
while(<FD2>) {#问题来啦:readline() on closed filehandle FD2
while ($word = <FD1>) {#问题也来啦:readline() on closed filehandle FD1
从代码上看,文件句柄确实已经打开了。百思不得其解。
文件句柄的文件路径也没有问题!
作者: py    时间: 2012-05-20 09:34
养成好习惯
  1. open FH, '<', './filename' or die $!;
复制代码

作者: tempo8    时间: 2012-05-20 20:56
本帖最后由 tempo8 于 2012-05-20 21:02 编辑
py 发表于 2012-05-20 09:34
养成好习惯

高人一语道破天机!
为么事绝对路径这么好使?!
可是怎么解决忽略中文的问题呢?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2