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