📜  珀尔 |告诉()函数

📅  最后修改于: 2022-05-13 01:55:03.636000             🧑  作者: Mango

珀尔 |告诉()函数

Perl 中的 tell()函数用于使用 FileHandle 获取读取指针在 File 中的位置。如果没有传递 FileHandle,则返回最近访问的文件中的位置。

示例 1:

#!/usr/bin/perl 
   
# Opening a File in Read-only mode 
open(fh, "<", "Hello.txt"); 
  
$position = tell(fh);
print("Position of read pointer before reading: $position");
   
# Reading first 10 characters from the file
for($i = 0; $i < 10; $i++)
{
    $ch = getc(fh);
}
  
$position = tell(fh);
  
# Current position of the read pointer
print("\nCurrent Position: $position");
   
# Closing the File 
close(fh); 

输出:

示例 2:

#!/usr/bin/perl 
   
# Opening a File in Read-only mode 
open(fh, "<", "Hello.txt"); 
  
$position = tell(fh);
print("Position of read pointer before reading: $position\n");
  
# Printing First 10 Characters
print("First ten characters are: ");
  
# Reading and printing first 
# 10 characters from the file
for($i = 0; $i < 10; $i++)
{
    $ch = getc(fh);
    print" $ch";
}
  
$position = tell(fh);
  
# Current position of the read pointer
print("\nCurrent Position: $position");
   
# Closing the File 
close(fh); 

输出: