当前位置:首页 » Perl » perl tell()函数

perl tell()函数

perl tell()函数例子,tell()函数实例代码 - 指定的文件句柄返回读指针的当前位置(以字节为单位)。

语法

tell FILEHANDLE

tell


定义和用法

指定的文件句柄返回读指针的当前位置(以字节为单位)。如果省略了文件句柄FILEHANDLE ,然后返回范围内访问的最后一个文件的位置。

返回值

  • 当前的文件位置(以字节为单位)

例子

要检查此函数,请在以下几点:

  • 创建一个文本文件,“这是测试”的内容,并把它存储到/ tmp目录。

  • 从这个文件中读取2个字符。

  • 现在,检查文件中的读指针的位置。

#!/usr/bin/perl -w
#by www.yibai.com
open( FILE, "</tmp/test.txt" ) || die "Enable to open test file";
$char = getc( FILE );
print "First Charctaer is $char\n";
$char = getc( FILE );
print "Second Charctaer is $char\n";
# Now check the poistion of read poiter.
$position = tell( FILE );
print "Position with in file $position\n";
close(FILE);

这将产生以下结果:

First Charctaer is T
Second Charctaer is h
Position with in file 2