in reply to read from a file and write into the same file
As you're opening for append, the file pointer is positioned at the end, so your while(<FILE>) does not read anything ('cos it's already at EOF). And, as your print FILE ... is in the same loop, it doesn't write anything either, because it doesn't execute at all...
BTW, it's generally a good idea to get into the habit of always error checking with file operations, in particular with open:
my $filename = "test.txt"; open FILE, "+>>", $filename or die "Couldn't open file '$filename': $! +";
Update: In short, what I think you want to achieve (read from the beginning, write at the end?) cannot be done that way, because a file handle does not maintain two file pointers... Alternatingly reading one line then writing (inserting) one line doesn't work either, because the write doesn't shift around the later parts of the file. Rather, it simply overwrites at that position, which typically doesn't work well with text files (due to different line lengths). To observe the effect try opening with "+<".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: read from a file and write into the same file
by ysth (Canon) on Mar 03, 2008 at 04:01 UTC | |
by almut (Canon) on Mar 03, 2008 at 07:43 UTC | |
by ysth (Canon) on Mar 03, 2008 at 07:49 UTC |