Thursday, October 1, 2015

Reading and Writing into a file using Perl Script

Reading a text file testfile.txt using Perl Script
#!c:\perl64\bin\perl.exe -w

use strict;
use warnings;

my $filePath = "F:\\Perl\\Perl\\testfile.txt";

open(FILE,"$filePath") or die "couldn't open the file, $!";

#Reading the Complete file (Slurping) and printing the content

while(<FILE>)

{

	print <FILE>;

}

close(FILE);



file content of testfile.txt:
Ataul	26	Male	0987
Biswa	23	Male	0654
Ruby	21	Female	0145
Sikha	20	Female	0345

Perl Script to write into a file

#!c:\perl64\bin\perl.exe -w

use strict;
use warnings;
my $filePath = "F:\\Perl\\Perl\\testfile.txt";

open(FILE,"+>>$filePath") or die "Couldn't open file,$!";

print "\n";

print FILE "Namo\t63\tMale\t0567\n";

close(FILE);


File Content after appending one more line into the testfile.txt
Ataul	26	Male	0987
Biswa	23	Male	0654
Ruby	21	Female	0145
Sikha	20	Female	0345
Namo	63	Male	0567

No comments:

Post a Comment