Home

ftruncate

ftruncate is a POSIX function that truncates or extends the size of the file associated with a given open file descriptor. It operates on the file described by the descriptor and is declared in unistd.h. The function returns 0 on success and -1 on error, with errno set to indicate the failure.

If the new length is smaller than the current size, the file is truncated to that length.

The file descriptor passed to ftruncate must refer to a file that supports resizing. It typically cannot

Prototype and usage: int ftruncate(int fd, off_t length); length is an off_t value representing the new size.

See also: truncate, file descriptor manipulation, and related filesystem resizing operations.

If
the
new
length
is
larger,
the
file
is
extended
to
that
size
and
the
added
region
is
filled
with
zero
bytes.
The
operation
affects
the
file’s
contents
up
to
the
specified
length;
bytes
beyond
that
length
are
discarded.
The
file
offset
is
not
required
to
be
modified
by
ftruncate.
be
a
directory,
pipe,
socket,
or
symbolic
link
opened
in
a
non-resizing
mode,
and
the
caller
must
have
appropriate
write
permissions
for
the
file.
On
error,
ftruncate
returns
-1
and
sets
errno
to
indicate
the
cause,
with
common
values
including
EBADF
(bad
file
descriptor),
EINVAL
(invalid
length
or
unsupported
operation),
EISDIR
(attempt
to
truncate
a
directory),
EACCES
(permissions
denied),
and
EFBIG
or
ENOSPC
(size
constraints
or
space
issues).
ftruncate
is
widely
supported
on
Unix-like
systems;
a
related
function
truncate
can
modify
a
file
by
path
rather
than
by
descriptor.