ITPub博客

首页 > Linux操作系统 > Linux操作系统 > eof函数

eof函数

原创 Linux操作系统 作者:yanyp 时间:2009-12-21 11:24:09 0 删除 编辑
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
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 --
$
    下面把eof改为eof(),第二个程序为:
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
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 --$
    这时,只有所有文件都读过了,eof()才返回真,如果只是多个文件中前几个的末尾,返回值为假,因为还有要读取的输入。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/230160/viewspace-623151/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 修改awr快照参数
请登录后发表评论 登录
全部评论

注册时间:2009-04-12

  • 博文量
    139
  • 访问量
    196027