23. Linux Truncate ftruncate function

Source: Internet
Author: User
Tags function definition

truncate ftruncate function

Both truncate () and ftruncate () can be used to modify the file size, but there are some differences between the two.

one, truncate () function

Header file:

#include <unistd.h>

function definition:

int truncate (const char *path, off_t length);

Function call:

Truncate ("/AAA", 500);

Function Description:

Truncate () Changes the file size specified by the parameter path to the size specified by the parameter length. If the original file size is larger than the parameter length, then the part that is exceeded is deleted

return value:

Successful execution returns 0, failure returns-1, error reason is stored in errno

Error code:

  1  eaccess parameter the file specified by path cannot be accessed   2  erofs the file to be written exists in the read-only file system   3  efault parameter The path pointer is out of the accessible space   4  einval parameter path contains illegal characters   5  enametoolong parameter path too long   6  enotdir parameter path path is not a directory   7   Eisdir parameter path points to a directory   8  etxtbusy Parameter path refers to a file that is a shared program and is being executed in   9  eloop parameter path There are too many symbolic connection problems.  10  EIO I/O access error 
Second, ftruncate () function

Header file:

#include <unistd.h>

function definition:

int ftruncate (int fd,off_t length);

Function call:

1 out =open ("/aaaa", o_rdwr|  O_CREAT,S_IRUSR); 2 ftruncate (out,$); 3 Close (out);

Function Description:

Ftruncate () Changes the file size specified by parameter FD to the size specified by parameter length.

The parameter fd is an open file descriptor and must be a file opened in write mode .

If the original file size is larger than the parameter length, then the excess will be deleted.

return value:

The execution succeeds returns 0, the failure returns-1, and the reason for the error is stored in errno.

Error code:

1 The EBADF parameter FD file descriptor is invalid or the file is closed.  2 EINVAL parameter FD is a socket not a file, or the file is not opened in write mode.

23. Linux Truncate ftruncate function

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.