ftruncate() — Truncate a file

Standards

Standards / Extensions C or C++ Dependencies
POSIX.1a
XPG4.2
Single UNIX Specification, Version 3
both  

Format

#define _POSIX1_SOURCE 2
#include <unistd.h>

int ftruncate(int fildes, off_t length);

General description

The ftruncate() function truncates the file indicated by the open file descriptor fildes to the indicated length. fildes must be a regular file that is open for writing. If the file size exceeds length, any extra data is discarded. If the file size is smaller than length, bytes between the old and new lengths are read as zeros. A change to the size of the file has no impact on the file offset.

Special behavior for XPG4.2: If the ftruncate() function would cause the file size to exceed the soft file size limit for the process, ftruncate() will fail and a SIGXFSZ signal will be generated for the process.

If successful, the ftruncate() function marks the st_ctime and st_mtime fields of the file.

If unsuccessful, the ftruncate() function leaves the file unchanged.

Large file support for z/OS® UNIX files: Large z/OS UNIX files are supported automatically for AMODE 64 C/C++ applications. AMODE 31 C/C++ applications must be compiled with the option LANGLVL(LONGLONG) and define the _LARGE_FILES feature test macro before any headers are included to enable this function to operate on z/OS UNIX files that are larger than 2 GB in size. File size and offset fields are enlarged to 63 bits in width. Therefore, any other function operating on the file is required to define the _LARGE_FILES feature test macro as well.

Returned value

If successful, the ftruncate() function returns 0.

If unsuccessful, the ftruncate() function returns -1 and sets errno to one of the following values:
Error Code
Description
EBADF
fildes is not a valid open file descriptor.
EFBIG
The length argument was greater than the maximum file size.
EINTR
Added for XPG4.2: A signal was caught during execution.
EINVAL
fildes does not refer to a regular file, it is opened read-only, or the length specified is incorrect.
EIO
Added for XPG4.2: An I/O error occurred while reading from or writing to a file system.
EROFS
The file resides on a read-only file system.

Example

CELEBF49
/* CELEBF49 */
#define _POSIX_SOURCE
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#undef _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>

#define string_len 1000

main() {
  char *mega_string;
  int  fd, ret;
  char fn[]="write.file";
  struct stat st;

  if ((mega_string = (char*) malloc(string_len)) == NULL)
    perror("malloc() error");
  else if ((fd = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    memset(mega_string, '0', string_len);
    if ((ret = write(fd, mega_string, string_len)) == -1)
      perror("write() error");
    else {
      printf("write() wrote %d bytes\n", ret);
      fstat(fd, &st);
      printf("the file has %ld bytes\n", (long) st.st_size);
      if (ftruncate(fd, 1) != 0)
        perror("ftruncate() error");
      else {
        fstat(fd, &st);
        printf("the file has %ld bytes\n", (long) st.st_size);
      }
    }
    close(fd);
    unlink(fn);
  }
}

Output
write() wrote 1000 bytes
the file has 1000 bytes
the file has 1 bytes

Related information