From: K-sPecial <[EMAIL PROTECTED]>
> Rajesh Dorairajan wrote:
> > Does anyone know of a way to open a file in append mode and append
> > on top of the file? 
> > 
> > Thanks in Advance,
> > 
> > --Rajesh 
> > 
> 
> I don't know why people are having a problem with this sollution,
> simply open in append mode so open doesn't clobber the file, then use
> seek() to move to the beginning of the file. Done :)

Could be ... because it doesn't work?

Try this:

#!perl
use Fcntl qw(SEEK_SET);
open OUT, '>', 'testAppend.txt' or die "Can't create testAppend.txt: 
$^E\n";
print OUT "Original content\nThis is the old stuff in the file you 
are trying to append to.\n";
close OUT;

print "------------------original content------------------\n";
open IN, '<', 'testAppend.txt';
while (<IN>) {print}
close IN;
print "----------------------------------------------------\n";

open APPEND, '>>', 'testAppend.txt' or die "Can't append to 
testAppend.txt: $^E\n";
seek APPEND, 0, SEEK_SET;
print APPEND "--K-sPecial says this will go on top.\n";
close APPEND;

print "------------------updated  content------------------\n";
open IN, '<', 'testAppend.txt';
while (<IN>) {print}
close IN;
print "----------------------------------------------------\n";
__END__

I get:


------------------original content------------------
Original content
This is the old stuff in the file you are trying to append to.
----------------------------------------------------
------------------updated  content------------------
Original content
This is the old stuff in the file you are trying to append to.
--K-sPecial says this will go on top.
----------------------------------------------------

Doesn't look like anything was "appended on top".

Think of a file like it was a sheet of paper. You may add more text 
after the end of the current one easily, you can erase part of the 
text and change it to a different one if the new text is as long as 
the old one or shorter. And if it's shorter you end up with some 
whitespace. But you can't squeeze more text on top or in the middle, 
you'd have to erase everything and write first the new text and then 
the original one.

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to