Zentara wrote:

> Hi,
> I'm losing my patience with this one.
> Can anyone tell me why in the following script,
> the second printout, is missing the first line?
> Why dosn't the second file seek reset the filehandle?
> ################################################
> #!/usr/bin/perl -w
> use strict;
> 
> my $file = 'test.txt';
> my $buffer;
> 
> open(FILE, "+<", $file) or die "Couldn't open $file: $!\n";
> 
> seek (FILE,0,0);
> my @lines = (<FILE>);
> print "@lines\n";             #this prints fine
> 
> seek (FILE,0,0);
>     while (<FILE>){read (FILE, $buffer, 1024);
>                   print "$buffer\n";     #this omits the first line
>                  }
> close FILE;
> exit  0;
> ##################################################
> 

that's because you discarded. the second seek() does reset the file pointer 
back to the beginning of your file but you then have:

while(<FILE>){
        read(FILE,$buffer,1024);
        print "$buffer\n";
}

the '<FILE>' reads a line from FILE but your are not using it. you then read 
another K with 'read(FILE,$buffer,1024)' and then prints it. as you can 
see, your '<FILE>' discard the first line! that's why you won't see the 
first line from your output. if you were to:

while(read(FILE,$buffer,1024) > 0){
        print "$buffer\n";
}

you should see the first line as well.

david





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to