TRUNCATE(2) | System Calls Manual | TRUNCATE(2) |
ftruncate
,
truncate
— truncate or
extend a file to a specified length
#include
<unistd.h>
int
ftruncate
(int fildes,
off_t length);
int
truncate
(const char *path,
off_t length);
ftruncate
()
and
truncate
()
cause the file named by path, or referenced by
fildes, to be truncated (or extended) to
length bytes in size. If the file size exceeds
length, any extra data is discarded. If the file size
is smaller than length, the file is extended and
filled with zeros to the indicated length. The
ftruncate
() form requires the file to be open for
writing.
Note:
ftruncate
()
and
truncate
()
do not modify the current file offset for any open file descriptions
associated with the file.
A value of 0 is returned if the call succeeds. If the call fails a -1 is returned, and the global variable errno specifies the error.
The ftruncate
() system call will fail
if:
EBADF
]EFBIG
]EINVAL
]EINVAL
]EROFS
]The truncate
() system call will fail
if:
EACCES
]EACCES
]EFAULT
]EISDIR
]ELOOP
]ENAMETOOLONG
]{NAME_MAX}
characters, or an entire path name exceeds
{PATH_MAX}
characters.ENOENT
]ENOTDIR
]EDEADLK
]EROFS
]ETXTBSY
]The ftruncate
() and
truncate
() system calls will fail if:
EFBIG
]EINTR
]EINVAL
]EIO
]EDEADLK
]These calls should be generalized to allow ranges of bytes in a file to be discarded.
Use of truncate
() to extend a file is not
portable.
The truncate
() and
ftruncate
() function calls appeared in
4.2BSD.
June 3, 2021 | BSD 4.2 |