ftruncate(), ftruncate64()

Updated: May 06, 2022

Truncate a file

Synopsis:

#include <unistd.h>

int ftruncate( int filedes,
               off_t length );

int ftruncate64( int filedes,
                 off64_t length );

Arguments:

filedes
The descriptor for the file that you want to truncate.
length
The length that you want the file to be, in bytes.

Library:

libc

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

Description:

The ftruncate() and ftruncate64() functions cause the file referenced by filedes to have a size of length bytes. The ftruncate64() function is a large-file support version of ftruncate().

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?

If the size of the file previously exceeded length, the extra data is discarded. If the size of the file was previously shorter than length, the file size is increased, and the extended area appears as if it were filled with NUL characters.

These functions don't modify the value of the seek pointer.

Upon successful completion, the ftruncate() function marks the st_ctime and st_mtime fields of the file for update. If the ftruncate() function is unsuccessful, the file is unaffected.

Returns:

Zero for success, or -1 if an error occurred (errno is set).

Errors:

EBADF
The filedes argument isn't a valid file descriptor.
EFBIG
The file is a regular file and length is greater than the offset maximum associated with the file.
EINTR
A signal was caught during the call to ftruncate().
EINVAL
The filedes argument doesn't refer to a file on which this operation is possible, the filedes argument isn't open for writing or the length argument is less than the minimum file size for the specified filesystem.
EIO
An I/O error occurred while reading from or writing to the filesystem.
ENOMEM
There wasn't enough memory to change the size of a shared memory object.
ENOSYS
The ftruncate() function isn't implemented for the filesystem specified by filedes.
ENOTSUP
The ftruncate() function is implemented for the specified filesystem, but the specific operation isn't supported.
EROFS
The file resides on a read-only filesystem.

Classification:

ftruncate() is POSIX 1003.1; ftruncate64() is Large-file support

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