Core\IO\File::seek
Moves the handle of an open file to another byte.
$file->seek($offset) works on a Core\IO\File that Core\IO::open returned. The next read or write then starts $offset bytes from the start of the file. seek(0) goes back to the start. The offset always counts from the start of the file. To move forward from the current position, use seek($file->tell() + $n).
An offset past the end of the file is allowed. A read there returns an empty string. A write there fills the gap before it with zero bytes.
A closed handle throws a RuntimeError. A handle that has no position, such as a pipe, throws an IOError.
Parameters
$offsetuintHow many bytes from the start of the file the next read or write happens at.
Return value
Returns void. Nothing. Use tell to get the position.
Errors
RuntimeErrorThe handle has already been closed.IOErrorThe seek itself failed. For example, the handle is a pipe or a terminal, which has no position.
Examples
Read the start twice
<?nvs// Shows how to move the handle of an open file, so the next read starts at another byte.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'greeting.txt');Core\IO::write($path, 'Hello, world');Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);
// This reads the first 5 bytes and prints "Hello". The handle is now after them.echo $file->read(5), "\n";
// `seek(0)` moves the handle back to the start, so this prints "Hello" again.$file->seek(0);echo $file->read(5), "\n";
// `seek(7)` moves the handle to byte 7, so this prints "world".$file->seek(7);echo $file->read(100), "\n";$file->close();HelloHelloworldRead one record
<?nvs// Shows how to read one record from a file where every record has the same size.// A record starts at its number times the record size.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'fruit.dat');// Every record is 10 bytes: 9 characters and a line break.Core\IO::write($path, "apple \nbanana \ncherry \ndate \n");Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);uint $size = 10;array<uint> $wanted = [2, 0];
// Record 2 starts at byte 20, and record 0 at byte 0. This prints "cherry" and "apple".foreach ($wanted as uint $number) { $file->seek($number * $size); echo Core\Str::trim($file->read($size)), "\n";}$file->close();cherryappleShow the end of a log
<?nvs// Shows how to read only the end of a large log file.// The program never reads the start of the file, however large it is.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'app.log');Core\IO\File $log = Core\IO::open($path, Core\IO::FileMode::Write);uint $i = 1;while ($i <= 5000) { $log->write('request ' . $i as string . " done\n"); $i = $i + 1;}$log->close();
// The last 60 bytes start at the file size minus 60.Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);$file->seek(Core\IO::size($path) - 60);string $end = $file->read(60);$file->close();
// The first line in `$end` is cut, so this prints only the lines after it.echo Core\Str::after($end, "\n");request 4998 donerequest 4999 donerequest 5000 done