On 5/15/07, Bruno Schroeder <[EMAIL PROTECTED]> wrote:
I tryed to use seek but it did not work. Can you help me on that, please? I
am using Windows XP. The following example writes at the end of the file.
use strict;
my $file = "teste_rw.txt";
open (FILE, "+<", $file) or die "Can not open $file: $!.";
for my $line (<FILE>) {
print $line;
seek(FILE, 0, 1);
print FILE "b\n";
seek(FILE, 0, 1);
}
my $a_while = 2;
sleep($a_while);
seek(FILE, tell(FILE), 0);
close FILE;
Here's some untested code that may do something resembling what you want:
my $file = "teste_rw.txt";
open (FILE, "+<", $file) or die "Can not open '$file' r/w: $!";
# $next_loc is the location of the next line to process
my $next_loc = tell FILE; # probably 0
while (1) {
my $current_loc = $next_loc;
# seek before each read or write
seek(FILE, $current_loc, 0) or die;
my $line = <FILE>;
last if not defined $line; # undef at eof
$next_loc = tell FILE;
print $line;
# Get the replacement string (somehow).
my $repl = &replacement_for($line);
die "Can't replace '$line' with '$repl'"
unless length($line) == length($repl);
# seek before each read or write
seek(FILE, $current_loc, 0) or die;
print FILE $repl;
}
close FILE;
I'm not sure why your code used sleep, so I omitted it. Cheers!
--Tom Phoenix
Stonehenge Perl Training
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/