perl script .. Help needed

Status
Not open for further replies.

smileysam

Member level 4
Joined
Feb 19, 2006
Messages
76
Helped
12
Reputation
24
Reaction score
3
Trophy points
1,288
Activity points
1,830
Iam trying to copy content of one file into another .
The code is not working and hangs and the content is not copied into file 2
I have pasted the code. Any comment will be useful . thanks.

# copy one file into another.
$len = $#ARGV + 1;
print($len);
print("$ARGV[0]\n");
print("$ARGV[1]\n");
if( (open(fileptr1,$ARGV[0])) == 0)
{
die("Error in opening the file");
}
else
{
print("File 1 opened\n");
}
print("Iam here");
if( (open(fileptr2,">>".$ARGV[1])) == 0)
{
die("Error in opening the file");
}
else
{
print("File 2 opened\n");
}
while(fileptr1 ne "")
{
$input = <fileptr1>;
print ($input);
print fileptr2 ($input);
}
close(fileptr1);
close(fileptr2);
 

This code works ok.

Code:
open(IN,$ARGV[0]) || die "Cannot open $ARGV[0] for reading: $! ";
open(OUT, ">$ARGV[1]") || die "Cannot create $ARGV[1]: $! ";

while(<IN>){
  print OUT $_;
  }

close(IN) || die "Cant close $ARGV[0]: $! ";
close(OUT) || die "Cant close $ARGV[1]: $! ";
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…