1. Dependent header Files
#include <unistd.h>
#include <sys/types.h>
2. Function definition:
int truncate (const char *path,off_t length);
int ftruncate (int fd,off_t length);
Function Description:
The truncate () and ftruncate () functions cause the regular file named
By path or referenced by FD to being truncated to a size of precisely
Length bytes.
Translation: the truncate () and ftruncate () functions cause a generic file named path or referred to by the file descriptor FD to be truncated to a file of length bytes in size.
If the file previously was larger than this size, the extra data is
Lost. If the file previously was shorter, it's extended, and the
Extended part reads as null bytes (' + ').
Translation: If the previous file is larger than this size, the additional data is lost. If the previous file is smaller than the currently defined size, then the file will be expanded, and the extended portion will be null, which is
If the size changed, then the St_ctime and St_mtime fields (respec‐
Tively, time of last status change and time of the last modification; See
Stat (2)) for the file is updated, and the Set-user-id and Set-group-id
Permission bits may be cleared.
Translation: If the size changes, then the St_ctime (Access time) and st_mtime () modification time will be updated.
With Ftruncate (), the file must is open for writing; With truncate (),
The file must be writable.
Translation: Using Ftruncate (), this file must be opened for write operations. Files that use the TRUNCATE function must be able to be written
3. Case Description:
Linux Server Programming: Truncate () function + case description