ftruncate(), ftruncate64()

Truncate a file

Synopsis:

#include <unistd.h>

int ftruncate( int fildes,
               off_t length );

int ftruncate64( int fildes,
                 off64_t length );

Arguments:

fildes
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:

These functions cause the file referenced by fildes to have a size of length bytes. If the size of the file previously exceeded length, the extra data is discarded (this is similar to using the F_FREESP option with fcntl()). If the size of the file was previously shorter than length, the file size is extended with NUL characters (similar to the F_ALLOCSP option to fcntl()).

The value of the seek pointer isn't modified by a call to ftruncate().

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 fildes 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 fildes 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.
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 (F_ALLOCSP or F_FREESP; see fcntl()) 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

See also:

mmap(), open(), shm_open(), truncate()