Skip to content

Core\IO\File::tell

$file->tell(): uint

Returns where the handle of an open file is.

$file->tell() works on a Core\IO\File that Core\IO::open returned. It returns the number of bytes from the start of the file to the handle. This is where the next read or write starts. A new handle is at 0, unless it was opened with Core\IO::FileMode::Append.

read, readLine and write move the handle past the bytes they used. tell counts bytes, not characters, so a character such as é counts as 2. The number tell returns can be given to seek later to come back to the same place.

A closed handle throws a RuntimeError. A handle that has no position, such as a pipe, throws an IOError.

Return value

Returns uint. The position, as a uint. Zero on a handle nothing has read or written yet, unless FileMode::Append put it at the end.

Errors

  • RuntimeErrorThe handle has already been closed.
  • IOErrorGetting the position failed. For example, the handle is a pipe or a terminal, which has no position.

Examples

Where the handle is

<?nvs
// Shows how to find out where the handle of an open file is.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'notes.txt');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Write);
// A new handle is at byte 0, so this prints 0.
echo $file->tell(), "\n";
// `write` moves the handle past what it wrote. This prints 6.
$file->write("first\n");
echo $file->tell(), "\n";
// This prints 13.
$file->write("second\n");
echo $file->tell(), "\n";
$file->close();
Output
0
6
13

The position counts bytes

<?nvs
// Shows that `tell` counts bytes, not characters.
// A character such as "é" uses 2 bytes.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'cafe.txt');
Core\IO::write($path, 'café au lait');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);
// "café" is 4 characters and 5 bytes, so this prints "café" and then 5.
echo $file->read(5), "\n";
echo $file->tell(), "\n";
$file->close();
Output
café
5

Read only new log lines

<?nvs
// Shows how to read only the lines that were added to a log file since the last time.
// The program saves the position with `tell`, and later starts there with `seek`.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'events.log');
Core\IO::write($path, "user 1 signed in\nuser 2 signed in\n");
// The first run reads every line, then saves where it stopped.
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);
?string $line = $file->readLine();
while ($line != null) {
echo 'first run: ', $line, "\n";
$line = $file->readLine();
}
uint $saved = $file->tell();
$file->close();
// Another program adds a line to the log.
Core\IO::append($path, "user 1 signed out\n");
// The next run starts at the saved position, so this prints only the new line.
$file = Core\IO::open($path, Core\IO::FileMode::Read);
$file->seek($saved);
$line = $file->readLine();
while ($line != null) {
echo 'next run: ', $line, "\n";
$line = $file->readLine();
}
$file->close();
Output
first run: user 1 signed in
first run: user 2 signed in
next run: user 1 signed out