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

首頁 > 編程 > Perl > 正文

perl讀寫文件代碼實(shí)例

2020-10-31 14:51:38
字體:
供稿:網(wǎng)友

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

Case 1: Throw an exception if you cannot open the file:

復(fù)制代碼 代碼如下:

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:

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 海安县| 绥中县| 长顺县| 仁化县| 洪雅县| 大田县| 通州区| 崇文区| 罗源县| 南川市| 修文县| 青海省| 湖州市| 巴彦淖尔市| 于都县| 安庆市| 开化县| 大名县| 洛南县| 凤山市| 井研县| 岱山县| 正安县| 沁源县| 隆回县| 客服| 林甸县| 茌平县| 宝鸡市| 比如县| 和静县| 武功县| 临湘市| 南昌县| 磐石市| 永平县| 许昌县| 永宁县| 涟源市| 三原县| 金川县|