eof函数查看最后一次读文件操作是否为文件最后一个记录,如果是,则返回非零值,如果文件还有内容,返回零。
一般情况下,对eof的调用不加括号,因为eof和eof()是等效的,但与<>操作符一起使用时,eof和eof()就不同了。现在我们来创建两个文件,分别叫做file1和file2。file1的内容为:
This is a line from the first file.
Here is the last line of the first file.
file2的内容为:
This is a line from the second and last file.
Here is the last line of the last file.
下面就来看一下eof和eof()的区别,第一个程序为:
一般情况下,对eof的调用不加括号,因为eof和eof()是等效的,但与<>操作符一起使用时,eof和eof()就不同了。现在我们来创建两个文件,分别叫做file1和file2。file1的内容为:
This is a line from the first file.
Here is the last line of the first file.
file2的内容为:
This is a line from the second and last file.
Here is the last line of the last file.
下面就来看一下eof和eof()的区别,第一个程序为:
1: #!/usr/local/bin/perl运行结果如下:
2:
3: while ($line = <>) {
4: print ($line);
5: if (eof) {
6: print ("-- end of current file --\n");
7: }
8: }
$ program file1 file2下面把eof改为eof(),第二个程序为:
This is a line from the first file.
Here is the last line of the first file.
-- end of current file --
This is a line from the second and last file.
Here is the last line of the last file.
-- end of current file --
$
1: #!/usr/local/bin/perl运行结果如下:
2:
3: while ($line = <>) {
4: print ($line);
5: if (eof()) {
6: print ("-- end of output --\n");
7: }
8: }
$ program file1 file2这时,只有所有文件都读过了,eof()才返回真,如果只是多个文件中前几个的末尾,返回值为假,因为还有要读取的输入。
This is a line from the first file.
Here is the last line of the first file.
This is a line from the second and last file.
Here is the last line of the last file.
-- end of output --$
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/230160/viewspace-623151/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/230160/viewspace-623151/
本文通过两个示例程序对比了Perl语言中eof与eof()函数的行为差异,特别是当它们用于判断文件读取状态时的不同表现。eof在读到文件末尾时立即返回真值,而eof()则在所有输入文件都被读完后才返回真。

1万+

被折叠的 条评论
为什么被折叠?



