FAT | RFS | XFS

ftruncate()

PROTOTYPE

#include <posix.h>

int ftruncate(int fid, off_t length);

DESCRIPTION

ftruncate() sets the length of the open regular file identified by fid to length characters. If the file is extended, ‘\0’ characters are appended. The file’s current position is set to the end of file.

If zeroing the extended area is unneeded, consider using fresize(). It is a non-standard routine that behaves like ftruncate() except if the file is extended, the extended area is left uninitialized. This can give higher performance.

If the file’s length is changed, its status change and data modification timestamps are updated.

If successful, ftruncate() returns 0. Otherwise, it sets errno and returns -1.

ERROR CODES

EBADF fid is not the descriptor of a file open in write or append mode.
EINVAL length is negative.
EISDIR The specified file is a directory.
ENOSPC No space is left on volume to write file data.
ENXIO The volume has been removed.
EROFS The volume is read-only.

EXAMPLE

    /*-----------------------------------------------------------------*/
    /* Shorten the file to half its length.                            */
    /*-----------------------------------------------------------------*/
    length = lseek(fid2, 0, SEEK_END);
    if (ftruncate(fid2, length / 2))
    {
      perror("ftruncate() failed");
      return -1;
    }