當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Perl tell()用法及代碼示例


Perl中的tell()函數用於通過其FileHandle獲取文件中讀取指針的位置。如果未傳遞FileHandle,則它將返回最近訪問的文件中的位置。

用法: tell(FileHandle)

參數:
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); 

輸出:



相關用法


注:本文由純淨天空篩選整理自Code_Mech大神的英文原創作品 Perl | tell() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。