A brief comparison of truncate () function and ftruncate () function _c language in C language

Source: Internet
Author: User
Tags error code

C language Truncate () function: Change file size
header file:

#include <unistd.h>

To define a function:

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

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, the portion of the excess is deleted.

Return value: Successful execution returns 0, failure returns-1, the reason for the error is in errno.

Error code:
1. The eaccess parameter path specifies a file that cannot be accessed.
2, Erofs to write the file exists in the read-only file system.
3, efault parameter The path pointer exceeds the accessible memory space.
4, einval parameter path contains illegal characters.
5, Enametoolong parameter path is too long.
6, enotdir parameter path path is not a directory.
7, the Eisdir parameter path points to a directory.
8, etxtbusy parameter path refers to the file as a shared program, and is being executed.
9, eloop parameter path has too many symbolic connection problems.
10, Eio I/O access errors.

C language Ftruncate () function: Change file size
header file:

#include <unistd.h>

To define a function:

int ftruncate (int fd, off_t length);

Function Description: ftruncate () changes the file size specified by the parameter fd to the size specified by the 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, the portion of the excess is deleted.

Return value: Successful execution returns 0, failure returns-1, the reason for the error is in errno.

Error code:
1, EBADF parameter FD file descriptor is invalid or the file has been closed.
2, einval parameter FD is a socket is not a file, or the file is not opened in write mode.

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.