大头
Table_bottom

标签云
Table_bottom

分类
Table_bottom

日历
二月
28293031123
45678910
11121314151617
18192021222324
252627282912
Table_bottom

评论
Table_bottom

留言
Table_bottom

微博
Table_bottom

热门文章
Table_bottom

随机文章
Table_bottom

豆瓣上谁关注这里
Table_bottom

链接
Table_bottom

搜索栏
Table_bottom

RSS
RSS Link
Table_bottom

功能
Table_bottom

页面
Table_bottom

计数器
463902
Table_bottom

访客统计
Table_bottom

存档
Table_bottom

初学Perl之五

loveisbug posted @ 2008年11月18日 01:21 in 娱乐 with tags Perl , 2053 阅读

今天要读取目录中的文件,把所有的txt文件打开用之前的程序分析一下。

先用的是foreach,后来改用while,运行报错,加打印信息看到readdir读进来的文件先是 . 和 .. 。不知道如何筛选目录下读出的文件,搜到一个perl学习笔记,那里找到了实例。

  1. next unless $file =~ /\.TXT$/;

现在可以筛选出所有的txt文件了。

但是有个问题,程序如下:

  1. opendir DH, $dir_to_process;
  2. while($file = readdir DH)
  3. #foreach $file (readdir DH)
  4. {
  5.         print $file, "\n";
  6.         next unless $file =~ /\.TXT$/;
  7.         print "one file in $dir_to_process is $file\n";
  8.         open(FILE, "<$file") or die "open error: $!";
  9.         while(<FILE>)
  10.         {
  11.                 #do something.
  12.         }
  13.         close(FILE);
  14. }

readdir读出一个文件,打印出文件名,然后看是否是txt文件,是的话打印“one file in xxx ……”,然后打开这个文件,一行一行读取。现象是这样,读取某一个文件的时候,报出了open error: No such file or directory。

而这个文件是存在,并且之前打印文件名,打印“one file in xxx ……”都有。可是在打开文件时却出错,说文件不存在,为什么?


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter