Results 1 to 3 of 3

Thread: Writing to files..

  1. #1
    Junior Member
    Join Date
    Jan 2006
    Location
    Cuttack, IND
    Posts
    18

    Writing to files..

    Hi,
    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
    ____________________________________________________________
    #>perl openpage.pl

    Welcome.. Enter a file name to rewrite:text.txt
    Cant write to olddata at openpage.pl line 13.
    _________________________________________________________

    #openpage.pl follows..


    use strict;


    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";
    foreach(@olddata){
    print or die"Cannot show old things";}
    print"\nEnter new data:\n";




    my @data=<>;


    open (CPY, ">>oldfile.txt") or die"Cant open to oldfile";


    print CPY @olddata or die"Cannot write to oldfile";
    print PAGE @data;
    close PAGE;
    close CPY;
    ______________________________________________________________________________
    Somu

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    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.

  3. #3
    Junior Member
    Join Date
    Jan 2006
    Location
    Cuttack, IND
    Posts
    18
    Thanx....
    Somu

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •