1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

I need to ADD to the BEGINNING of a data file

Discussion in 'Perl' started by fredfletcher, Feb 14, 2012.

  1. fredfletcher

    fredfletcher New Member

    Joined:
    Sep 12, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    I have done my best in trying to find a way to be able to do the following in Perl.

    I have a text file (data.txt) that I would like to add some info at the beginning of the file (prepend I believe, not append).

    From what I have gathered, "append" adds to the "end" of a file. I need to add to the "beginning" of a file after a form submission.

    Any help would be appreciated, thank you.

    Here is what I have so far:
    Code:
    #!/usr/bin/perl -w
    
    use CGI ':standard';
    
    open WRITE, "<data.txt";
    
    # 'name' is what is taken from my submission form
    $name = param('name'); 
    
    open(WRITE, ">>./data.txt");
    
    print WRITE "$name\n";
    
    close WRITE;
    
    print "Content-type: text/html\n\n";
    print "Success!"; 
    
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
  3. TedH

    TedH New Member

    Joined:
    Apr 11, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Try this:

    Code:
    use strict;
    use warnings;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    print "Content-type: text/html\n\n";
    my $dbfile = "data.txt";
    my $newline = "aaaaa";
    open DBF, "<$dbfile";
    flock(DBF, 2);
    read DBF, my $data, 1000000;
    close DBF;
    
    open DBF, ">$dbfile"; flock(DBF, 2);  
     print DBF $newline, "\n" . $data;
    close DBF;
    exit(0:
    For test, just change aaaaa to bbbbb etc.

    Hope that helps
     

Share This Page