| Author |
Writing at exact location in file
|
|
| rahulthathoo 2006-11-02, 9:57 pm |
| Hi
I want to write to a file - which already exits and has some
data in it. I dont want to erase that data. Say suppose it has 30
lines of text in it. I want to write something in the first line after
the first two words. How do i go about it. I know how to open a file
but I am not able to s to the right position and not able to write
my text without deleting the entire data of the file but i dont want
that. Can you help me here.
Thanks
Rahul
| |
| FishMonger 2006-11-02, 10:51 pm |
| quote: Originally posted by rahulthathoo
Hi
I want to write to a file - which already exits and has some
data in it. I dont want to erase that data. Say suppose it has 30
lines of text in it. I want to write something in the first line after
the first two words. How do i go about it. I know how to open a file
but I am not able to s to the right position and not able to write
my text without deleting the entire data of the file but i dont want
that. Can you help me here.
Thanks
Rahul
I'd suggest using the Tie::File module.
http://search.cpan.org/~mjd/Tie-Fil...lib/Tie/File.pm | |
| Ram Prasad 2006-11-03, 7:57 am |
| rahulthathoo wrote:
> Hi
> I want to write to a file - which already exits and has some
> data in it. I dont want to erase that data. Say suppose it has 30
> lines of text in it. I want to write something in the first line after
> the first two words. How do i go about it. I know how to open a file
> but I am not able to s to the right position and not able to write
> my text without deleting the entire data of the file but i dont want
> that. Can you help me here.
>
> Thanks
> Rahul
When editing a file ( not appending ) , wether you use a perl script or
an editor you are going rewrite the entire file.
You only feel you are inserting text inbetween
for your case you could use inplace-edit ( perldoc perlvar )
{
local(@ARGV)=($file);
local($^I)='~';
my $i =0 ;
while(<> ){
if(++$i = $line ) {
print "Inserted text";
}
print;
}
}
| |
| rahulthathoo 2006-11-03, 7:57 am |
| Thanks!
Ram Pra wrote:
> rahulthathoo wrote:
> When editing a file ( not appending ) , wether you use a perl script or
> an editor you are going rewrite the entire file.
> You only feel you are inserting text inbetween
>
> for your case you could use inplace-edit ( perldoc perlvar )
> {
> local(@ARGV)=($file);
> local($^I)='~';
> my $i =0 ;
> while(<> ){
> if(++$i = $line ) {
> print "Inserted text";
> }
> print;
> }
> }
|
|
|
|