略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: SplFileObject::fseek

2024-11-15

SplFileObject::fseek

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

SplFileObject::fseekSeek to a position

说明

public SplFileObject::fseek(int $offset, int $whence = SEEK_SET): int

Seek to a position in the file measured in bytes from the beginning of the file, obtained by adding offset to the position specified by whence.

参数

offset

The offset. A negative value can be used to move backwards through the file which is useful when SEEK_END is used as the whence value.

whence

whence values are:

  • SEEK_SET - Set position equal to offset bytes.
  • SEEK_CUR - Set position to current location plus offset.
  • SEEK_END - Set position to end-of-file plus offset.

If whence is not specified, it is assumed to be SEEK_SET.

返回值

Returns 0 if the seek was successful, -1 otherwise. Note that seeking past EOF is not considered an error.

范例

示例 #1 SplFileObject::fseek() example

<?php
$file 
= new SplFileObject("somefile.txt");

// Read first line
$data $file->fgets();

// Move back to the beginning of the file
// Same as $file->rewind();
$file->fseek(0);
?>

参见

  • fseek() - 在文件指针中定位
add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/splfileobject.fseek.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3