Truncate a file to a specified length
int truncate( const char* path,
off_t length );
- The path name of the file that you want to truncate.
- The new size of the file.
Use the -l c option to
to link against this library.
This library is usually included automatically.
The truncate() function causes the regular file named by
path to have a size of length bytes.
The effect of truncate() on other types of
files is unspecified. If the file previously was larger
than length, the extra data is lost. If it was previously
shorter than length, bytes between the old and new lengths
are read as zeroes. The process must have write
permission for the file.
If the request would cause the file size to exceed the soft
file size limit for the process, the request fails and
the implementation generates the SIGXFSZ signal for the
This function doesn't modify the file offset for any open
file descriptions associated with the file. On successful
completion, if the file size is changed, truncate()
marks for update the st_ctime and st_mtime fields of the
file, and if the file is a regular file, the S_ISUID and
S_ISGID bits of the file mode may be cleared.
- An error occurred; errno
- A component of the path prefix denies search permission, or write
permission is denied on the file.
- The path argument points outside the process's
allocated address space.
- The length argument was greater than the maximum file size.
- A signal was caught during execution.
- The length argument is invalid, or the path argument isn't an ordinary file.
- An I/O error occurred while reading from or writing to a filesystem.
- The named file is a directory.
- Too many symbolic links were encountered in resolving path.
- The maximum number of file descriptors available to the process has
- Components of path require hopping to multiple remote
machines and filesystem type doesn't allow it.
- The length of the specified pathname exceeds
PATH_MAX bytes, or the length of a component
of the pathname exceeds NAME_MAX bytes.
- Additional space couldn't be allocated for the system file table.
- A component of path doesn't name an existing
file or path is an empty string.
- The path argument points to a remote machine
and the link to that machine is no longer active.
- A component of the path prefix of path isn't a directory.
- The named file resides on a read-only filesystem.
POSIX 1003.1 XSI