Changes file length to a specified length, in bytes. Format #include <unistd.h> int truncate (const char *path, off_t length);
1 – Arguments
path The name of a file that is to be truncated. This argument must point to a pathname that names a regular file for which the calling process has write permission. length The new length of the file, in bytes. The off_t type of length is either a 64-bit or 32-bit integer. The 64-bit interface allows for file sizes greater than 2 GB, and can be selected at compile time by defining the _LARGEFILE feature-test macro as follows: CC/DEFINE=_LARGEFILE
2 – Description
The truncate function changes the length of a file to the size, in bytes, specified by the length argument. If the new length is less than the previous length, the function removes all data beyond length bytes from the specified file. All file data between the new End-of-File and the previous End-of- File is discarded. For stream files, if the new length is greater than the previous length, new file data between the previous End-of-File and the new End-of-File is added, consisting of all zeros. (For record files, it is not possible to extend the file in this manner.)
3 – Return Values
0 Indicates success. -1 An error occurred; errno is set to indicate the error.