perl中seek函数的用法

标签:
perlseek用法杂谈 |
分类: perl语言学习 |
seek FILEHANDLE,POSITION,WHENCE
POSITION 是读入的新位置(字节)。
WHENCE有3个值,0表示新位置是POSITION,1表示当前位置加上POSITION,2表示文件尾加上POSITION
例如:从file.txt的12字节开始读起并打印出来。
open (FILEHANDLE,"<file.txt") or die "cannot open file.txt";
seek FILEHANDLE,12,0;
while (<FILEHANDLE>){
}
close (FILEHANDLE);
http://www.purlgurl.net/%7Epurlgurl/graphics/aniline.gif
open (TEST, "<E:/test.txt");
while (<TEST>) {
}
exit;
test.txt:
skip this line
A keyword is here
print this line
Trick location of keyword
print this line
print this line
print this line
skip this line
skip this line
This is another keyword
print this line
print this line
print this line
skip this line
Last line and a keyword
PRINTED
RESULTS:
A keyword is here