您可以使用fseek()清除流上的eof条件.基本上,读到文件的末尾,睡眠一段时间,fseek()(不改变你的位置)清除eof,再次读取到文件结尾.洗,冲洗,重复.man fseek(3)了解详情.
这是perl中的样子.perl的seek()本质上是fseek(3)的包装器,所以逻辑是一样的:
wembley 0 /home/jj33/swap >#> cat p
my $f = shift;
open(I, "<$f") || die "Couldn't open $f: $!\n";
while (1) {
seek(I, 0, 1);
while (defined(my $l = <I>)) {
print "Got: $l";
}
print "Hit EOF, sleeping\n";
sleep(10);
}
wembley 0 /home/jj33/swap >#> cat tfile
This is
some
text
in
a file
wembley 0 /home/jj33/swap >#> perl p tfile
Got: This is
Got: some
Got: text
Got: in
Got: a file
Hit EOF, sleeping
Run Code Online (Sandbox Code Playgroud)
然后,在另一个会话中:
wembley 0 /home/jj33/swap > echo "another line of text" >> tfile
Run Code Online (Sandbox Code Playgroud)
并回到原始程序输出:
Hit EOF, sleeping
Got: another line of text
Hit EOF, sleeping
Run Code Online (Sandbox Code Playgroud)