Seek and tell. : file seek « File « Perl






Seek and tell.

    
#!/usr/local/bin/perl 

@array = ("This", "is", "a", "test"); 
open (TEMPFILE, ">file1"); 
foreach $element (@array) { 
    print TEMPFILE ("$element\n"); 
} 
close (TEMPFILE); 
open (TEMPFILE, "file1"); 
while (1) { 
    $skipback = tell(TEMPFILE); 
    $line = <TEMPFILE>; 
    last if ($line eq ""); 
    print ($line); 
    $line = <TEMPFILE>; # assume the second line exists 
    print ($line); 
    seek (TEMPFILE, $skipback, 0); 
    $line = <TEMPFILE>; 
    print ($line); 
    $line = <TEMPFILE>; 
    print ($line); 
} 

   
    
    
    
  








Related examples in the same category

1.Seek a file
2.Seek a minus value
3.Seek current position
4.Seek log file
5.Using seek function from IO:File
6.The seek Function randomly accesses a file.
7.The seek Function randomly accesses a file: seek(FILEHANDLE, BYTEOFFSET, FILEPOSITION);
8.File seek operations
9.Move file pointer by using the seek function