You are on page 1of 3

Priyankar Paul 1

29SCS124

Ex. No: 7
23/03/2011 FILE COPY

Aim:
To write a PERL program to copy the contents of one file to another file.

Algorithm:
Create and open the file1 in the write mode.

Read the contents from the user and write into file1 and close file1.

Create and open the file2 in the write mode and open file1 in read mode.

Read the contents from file1 and write into file2.

Close file1 and file2.


Priyankar Paul 2
29SCS124

Program:

print ("Enter the name: ");


$name=<stdin>;
print ("Enter the rollno: ");
$rollno=<stdin>;
open(File,">Demo.txt");
print File "$name";
print File "$rollno";
close (File);
print("\nThe contents of the file are as printed below\n");
open(Read_File,'Demo.txt');
$read=<Read_File>;
while($read)
{
print ("$read");
$read=<Read_File>;
}
close (Read_File);
open(Write_File,'>Output.txt');
open(Reading,'Demo.txt');
$read=<Reading>;
while($read)
{
print Write_File "$read";
$read=<Reading>;
}
close (Reading);
close (Write_File);
Priyankar Paul 3
29SCS124

Output:

Enter the name: Priyankar


Enter the rollno: 29scs124

The contents of the file are as printed below


Priyankar
29scs124

Result:

Thus the PERL program to copy the contents of one file to another file was successfully
executed and its output was verified.

You might also like