Skip to content

Core\IO\File::truncate

$file->truncate(uint $size): void$file->truncate(
    uint $size,
): void

Sets the length of an open file.

$file->truncate($size) works on a Core\IO\File that Core\IO::open returned. After it, the file is $size bytes long. A smaller size deletes every byte after $size. A larger size adds zero bytes at the end. truncate(0) empties the file.

truncate does not move the handle. If the handle was after the new end, it stays there, and the next write leaves a gap of zero bytes before it. To write from the start of an emptied file, call seek(0) after truncate(0).

A closed handle throws a RuntimeError. A handle opened with Core\IO::FileMode::Read throws an IOError, because the file cannot be changed through it.

Parameters

  • $sizeuintHow long the file is to be afterwards, in bytes from its start.

Return value

Returns void. Nothing. The handle's position does not move. After a file is shortened, the position can be past its new end. tell still returns the old position. A write there leaves a gap filled with zeroes before the written bytes.

Errors

  • RuntimeErrorThe handle has already been closed.
  • IOErrorThe resize itself failed. The most common cause is a handle opened with FileMode::Read, which the operating system does not resize.

Examples

Shorten a file

<?nvs
// Shows how to make a file shorter. `truncate` deletes every byte after the new size.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'list.txt');
Core\IO::write($path, "milk\nbread\neggs\n");
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::ReadWrite);
// The first two lines are 11 bytes. This keeps them and deletes "eggs".
$file->truncate(11);
$file->close();
// This prints "milk" and "bread".
echo Core\IO::read($path);
Output
milk
bread

A larger size adds zero bytes

<?nvs
// Shows that `truncate` can also make a file longer. The new bytes are zero bytes.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'space.dat');
Core\IO::write($path, 'abc');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::ReadWrite);
// This prints 3, then 1024. The file now has 1021 zero bytes after "abc".
echo Core\IO::size($path), "\n";
$file->truncate(1024);
echo Core\IO::size($path), "\n";
// `truncate` does not move the handle, so this prints 0.
echo $file->tell(), "\n";
$file->close();
Output
3
1024
0

Replace a state file

<?nvs
// Shows how to replace the whole content of a file that stays open and locked.
// A job does this to save its progress. The lock keeps out other programs that
// also lock the file, so they never read it half-written.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'job-state.txt');
Core\IO::write($path, 'last order: 1041, status: running');
Core\IO\File $state = Core\IO::open($path, Core\IO::FileMode::ReadWrite);
$state->lock();
// `truncate(0)` empties the file, and `seek(0)` moves the handle to the start.
// Without `seek(0)`, the next write would start after a gap of zero bytes.
$state->truncate(0);
$state->seek(0);
$state->write('last order: 1042, status: done');
$state->close();
// This prints the new content only.
echo Core\IO::read($path), "\n";
Output
last order: 1042, status: done