Linux Server Programming: Truncate () function + case description

Source: Internet
Author: User
Tags function definition truncated

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.