Truncate a file at a given position


#include <sys/types.h>
#include <unistd.h>

off_t ltrunc( int fildes, 
              off_t offset,
              int whence );


The file descriptor of the file that you want to truncate.
The relative offset from the file position determined by the whence argument.
The position in the file. The possible values (defined in <unistd.h>) are:
The new file position is computed relative to the current file position. The value of offset may be positive, negative or zero.
The new file position is computed relative to the end of the file.
The new file position is computed relative to the start of the file. The value of offset must not be negative.



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


The ltrunc() function attempts to truncate the file at a specified position. The file, referenced by the open file descriptor fildes, must have been opened O_WRONLY or O_RDWR. The truncation point is calculated using the value of offset as a relative offset from a file position determined by the value of the argument whence. The value of offset may be negative, although a negative truncation point (one before the beginning of the file) is an error.

Note: The ltrunc() function ignores advisory locks that may have been set by fcntl().

The calculated truncation point, if within the existing bounds of the file, determines the new file size; all data after the truncation point no longer exists. If the truncation point is past the existing end of file, the file size isn't changed. An error occurs if you attempt to truncate before the beginning of the file (that is, a negative truncation point).

Note: The current seek position isn't changed by this function under any circumstance, including the case where the current seek position is beyond the truncation point.


Upon successful completion, this function returns the new file size. If a truncation point beyond the existing end of file was specified, the existing file size is returned, and the file size remains unchanged. Otherwise, ltrunc() returns a value of -1 and sets errno to indicate the error. The file size remains unchanged in the event of an error.


The fildes argument isn't a valid file descriptor, open for writing.
The whence argument isn't a proper value, or the resulting file size would be invalid.
An attempt was made to truncate a file of a type that doesn't support truncation (for example, a file associated with the device manager).
The fildes argument is associated with a pipe or FIFO.


#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>

char buffer[1000];

int main( void )
    int fd, stat;

    fd = open( "test", O_CREAT | O_RDWR, 0666 );
    if( fd == -1 ) {
        fprintf( stderr, "Open error\n" );
        exit( -1 );

    /* Create a 1000-byte file */
    write( fd, buffer, 1000 );

    /* Seek back to offset 500 and truncate the file */
    if( ltrunc( fd, 500, SEEK_SET ) == -1 ) {
        fprintf( stderr, "ltrunc error\n" );
        exit( -1 );
    close( fd );
    fd = open( "test", O_CREAT | O_RDWR, 0666 );
    printf( "File size = %ld\n",
        lseek( fd, 0, SEEK_END ) );
    close( fd );

    return 0;



Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes


The ltrunc() function isn't portable, and shouldn't be used in new code. Use ftruncate() instead.