December 7th, 2006, 10:24 PM
Writing to files..
please help me out with this code.
I want to enter new text in a file specified by the user, and transfer the old data into another file named oldfile.txt
It only writes to the specified file, but doesn't save the old data in oldfile.txt
Here is the result on execution
Welcome.. Enter a file name to rewrite:text.txt
Cant write to olddata at openpage.pl line 13.
print"\nWelcome.. Enter a file name to rewrite:" or die"Cant ask";
my $file=<> or die"Cant ask 2";
open (PAGE,">$file") || die "Cannot open file.";
my @olddata=<PAGE> or die"Cant write to olddata";
print "The file contains the following data:\n" or die"cannot show";
print or die"Cannot show old things";}
print"\nEnter new data:\n";
open (CPY, ">>oldfile.txt") or die"Cant open to oldfile";
print CPY @olddata or die"Cannot write to oldfile";
print PAGE @data;
December 9th, 2006, 07:48 PM
You need to open the file they enter for reading first, then read the data. Then you can close it, and reopen it for writing to write the new data. Then do what you were doing with writing the old and new data. I'm guessing it's failing because you're trying to read from a file opened for writing. Good luck.
December 10th, 2006, 10:59 AM