Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

writing to top of a file

by Anonymous Monk
on Aug 06, 2001 at 21:08 UTC ( [id://102508]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

is there a way i can append a line to the TOP of a file, rather than the bottom, quickly? i know i could put every line of the file into an array, and put one line above the rest of the array, but is there a way around that?

Replies are listed 'Best First'.
Re: writing to top of a file
by abstracts (Hermit) on Aug 06, 2001 at 21:26 UTC
    Hello

    There are two ways you can add a line to the top of file:

    1. Read the entire file into memory, add your line, write back.
    2. Open a new file, add your line, open the old file, dump its content to the new file, close both files and rename.
    Any other way will be using one of these 2 ways.

    Aziz,,,

Re: writing to top of a file
by koolade (Pilgrim) on Aug 06, 2001 at 23:22 UTC
Re: writing to top of a file
by foogod (Friar) on Aug 06, 2001 at 22:09 UTC

    example code would be ...

    open (FH, "<filename.txt") || die "could not open file: $!"; @ODB = <FH>; close (FH); $newline = "The new line of text to go at the top of the file"; open (NFH, ">filename.txt") || die "could not open file 2: $!"; print NFH "$newline" . "\n"; foreach $line (@ODB) { print NFH "$line"; } close (NFH);

    There are so many different ways to do it ... it is crazy to try and put them all here. The example above is ok, but if the file is very large, you might want to use join the data.

    HTH,

    - f o o g o d

    --- ruining the bell curve for everyone else ---

      Hello

      You said: The example above is ok, but if the file is very large, you might want to use join the data.

      Joining the data will not eleminate the problem, it will only delay it (and not by much for that matter). If the file is very large, use:

      use File::Temp qw/tempfile/; open $FIN, "<$file1" or die .... my ($fh, $fname) = tempfile(); print $fh "FIRST LINE\n"; print $fh $_ while <$FIN>; close $FIN; close $fh; rename $fname, $file1;
      Code to check success of operations is not included for readability

      Aziz,,,

      Update: Thanks runrig for the comment. The point is the same, don't read all data into memory when moving data from file to file.

        Just use File::Copy (more efficient than reading/writing one line at a time):
        use File::Temp qw/tempfile/; use File::Copy; my ($fh, $fname) = tempfile(); syswrite $fh, "FIRST LINE\n"; copy($file1, $fh) or die "Error copying: $!"; close $fh;
        rename $fname, $file1;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://102508]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2024-04-25 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found