Truncate a file
#include <unistd.h> int ftruncate( int fildes, off_t length ); int ftruncate64( int fildes, off64_t length );
- The descriptor for the file that you want to truncate.
- The length that you want the file to be, in bytes.
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The ftruncate() and ftruncate64() functions cause the file referenced by fildes to have a size of length bytes. The ftruncate64() function is a 64-bit version of ftruncate().
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()).
These functions don't modify the value of the seek pointer.
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.
Zero for success, or -1 if an error occurred ( errno is set).
- The fildes argument isn't a valid file descriptor.
- The file is a regular file and length is greater than the offset maximum associated with the file.
- A signal was caught during the call to ftruncate().
- 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.
- An I/O error occurred while reading from or writing to the filesystem.
- There wasn't enough memory to change the size of a shared memory object.
- The ftruncate() function isn't implemented for the filesystem specified by filedes.
- The ftruncate() function is implemented for the specified filesystem, but the specific operation (F_ALLOCSP or F_FREESP; see fcntl()) isn't supported.
- The file resides on a read-only filesystem.
Last modified: 2014-06-24