truncate(), truncate64()

Updated: April 19, 2023

Truncate a file to a specified length

Synopsis:

#include <unistd.h>

int truncate( const char* path,
              off_t length );

int truncate64( const char* path,
                off64_t length );

Arguments:

path
The path name of the file that you want to truncate.
length
The new size of the file.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The truncate() and truncate64() functions cause the regular file named by path to have a size of length bytes. The truncate64() function is a large-file support version of truncate().

Note: In QNX Neutrino 6.6 or later, the large-file support functions and data types appear in the name space only if you define _LARGEFILE64_SOURCE when you compile your code. For more information, see Classification in What's in a Function Description?

The effect of truncate() on other types of files is undefined. 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.

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.

Returns:

0
Success.
-1
An error occurred; errno is set.

Errors:

EACCES
A component of the path prefix denies search permission, or write permission is denied on the file.
EFAULT
The path argument points outside the process's allocated address space.
EFBIG
The length argument was greater than the maximum file size.
EINTR
A signal was caught during execution.
EINVAL
The length argument is invalid, or the path argument isn't an ordinary file.
EIO
An I/O error occurred while reading from or writing to a filesystem.
EISDIR
The named file is a directory.
ELOOP
Too many symbolic links were encountered in resolving path.
EMFILE
The maximum number of file descriptors available to the process has been reached.
EMULTIHOP
Components of path require hopping to multiple remote machines and filesystem type doesn't allow it.
ENAMETOOLONG
The length of the specified pathname exceeds PATH_MAX bytes, or the length of a component of the pathname exceeds NAME_MAX bytes.
ENFILE
Additional space couldn't be allocated for the system file table.
ENOENT
A component of path doesn't name an existing file or path is an empty string.
ENOLINK
The path argument points to a remote machine and the link to that machine is no longer active.
ENOTDIR
A component of the path prefix of path isn't a directory.
EROFS
The named file resides on a read-only filesystem.

Classification:

truncate() is POSIX 1003.1; truncate64() is Large-file support

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes