qiuri2008

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
1)文件读取的3中方法
 
按行读,存入标量
while (<FILE>) { print; }
按行读,存入数组
@array = <FILE>;
读入整个文件 ,存入标量
$string = do { local $/; <FILE>; };
 
2)读文件实例
open (EP,"/etc/passwd");
while (<EP>) {
chomp;
print "I saw $_ in the password file!\n";
}
 
3)读写文件实例
open(IN,$a) || die "cannot open $a for reading: $!";
open(OUT,">$b") || die "cannot create $b: $!";
while (<IN>) { # read a line from file $a into $_
print OUT $_; # print that line to file $b
}
close(IN) || die "can't close $a: $!";
close(OUT) || die "can't close $b: $!";
posted on 2016-11-02 20:28  江召伟  阅读(1510)  评论(0编辑  收藏  举报