国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Perl > 正文

perl讀寫文件代碼實例

2020-06-04 20:32:39
字體:
來源:轉載
供稿:網友

#mode operand create truncate
#read < 
#write >  yes yes 
#append >> yes

Case 1: Throw an exception if you cannot open the file:
代碼如下:
use strict;
use warnings;
 
my $filename = 'data.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' with the error $!";
 
while (my $row = <$fh>) {
chomp $row;
print "$row/n";
}
close($fh);
  

Case 2: Give a warning if you cannot open the file, but keep running:
代碼如下:
use strict;
use warnings;
 
my $filename = 'data.txt';
if (open(my $fh, '<:encoding(UTF-8)', $filename)) {
while (my $row = <$fh>) {
chomp $row;
print "$row/n";
}
close($fh);
} else {
warn "Could not open file '$filename' $!";
}
  

Case 3: Read one file into array
代碼如下:
use strict;
use warnings;
 
my $filename = 'data.txt';
open (FILEIN, "<", $filename)
or die "Could not open file '$filename' with the error $!";
my @FileContents = <FILEIN>;
for my $l (@FileContents){
print "$l/n";
}
close FILEIN;
  

end

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 利津县| 聂荣县| 巩留县| 安义县| 西乌珠穆沁旗| 印江| 金华市| 甘南县| 黔西| 长宁县| 仪征市| 达拉特旗| 太湖县| 疏附县| 万山特区| 文水县| 抚顺市| 丹东市| 尼勒克县| 桓仁| 河西区| 白沙| 浪卡子县| 丰都县| 乳源| 林州市| 武安市| 金山区| 修武县| 武汉市| 尼玛县| 临朐县| 平远县| 盐边县| 新建县| 竹北市| 丽水市| 合山市| 宿州市| 拉萨市| 忻城县|