国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 合江县| 余姚市| 北海市| 兴宁市| 七台河市| 丰都县| 新绛县| 城步| 天等县| 灌云县| 凌源市| 仁寿县| 阜康市| 泗洪县| 宜章县| 呈贡县| 南华县| 宣恩县| 柳林县| 海兴县| 财经| 萨迦县| 社旗县| 伊春市| 沙田区| 肇庆市| 康乐县| 咸宁市| 寿阳县| 宜城市| 克什克腾旗| 九寨沟县| 井冈山市| 德令哈市| 南昌县| 黎川县| 岱山县| 屏山县| 和田市| 法库县| 金川县|