#include <unistd.h> int ftruncate(int file_descriptor, off_t length);Service Program Name: QP0LLIB1
The ftruncate() function truncates the file indicated by the open file descriptor file_descriptor to the indicated length. file_descriptor must be a "regular file" that is open for writing. (A regular file is a stream file that can support positioning the file offset.) If the file size exceeds length, any extra data is discarded. If the file size is smaller than length, the file is extended and filled with binary zeros to the indicated length. (In the QSYS.LIB and independent ASP QSYS.LIB file systems blanks are used instead of zeros to pad records after a member is extended.) The ftruncate() function does not modify the current file offset for any open file descriptions associated with the file.
If ftruncate() completes successfully, it marks the change time and modification times of the file. Also, the S_ISUID (set-user-ID) and S_ISGID (set-group-ID) bits of the file mode are cleared. If ftruncate() is not successful, the file is unchanged.
If ftruncate() is used to truncate the file to 0 bytes and the file has a digital signature, the signature is deleted.
No authorization is required. Authorization is verified during open() or creat().
If ftruncate() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
Error condition | Additional information |
---|---|
[EACCES] |
If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems. |
[EAGAIN] | |
[EBADF] | |
[EBADFID] | |
[EBADNAME] | |
[EBUSY] |
The QSYS.LIB or independent ASP QSYS.LIB file system cannot get exclusive access to the member to clear truncated data. |
[ECONVERT] | |
[EDAMAGE] | |
[EFBIG] |
The size of the object would exceed the system allowed maximum size or the process soft file size limit. The file is a regular file and length is greater than 2GB minus 1 byte. |
[EINTR] | |
[EINVAL] |
For example, file_descriptor does not refer to a regular file open for writing, or the specified length is not correct. |
[EIO] | |
[EISDIR] | |
[EJRNDAMAGE] | |
[EJRNENTTOOLONG] | |
[EJRNINACTIVE] | |
[EJRNRCVSPC] | |
[ELOCKED] | |
[ENAMETOOLONG] | |
[ENEWJRN] | |
[ENEWJRNRCV] | |
[ENOENT] | |
[ENOMEM] | |
[ENOSPC] | |
[ENOSYS] | |
[ENOSYSRSC] | |
[ENOTAVAIL] | |
[ENOTDIR] | |
[ENOTSAFE] | |
[ENOTSUP] | |
[EROOBJ] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[EUNKNOWN] |
If interaction with a file server is required to access the object, errno could also indicate one of the following errors:
Error condition | Additional information |
---|---|
[EADDRNOTAVAIL] | |
[ECONNABORTED] | |
[ECONNREFUSED] | |
[ECONNRESET] | |
[EHOSTDOWN] | |
[EHOSTUNREACH] | |
[ENETDOWN] | |
[ENETRESET] | |
[ENETUNREACH] | |
[ETIMEDOUT] | |
[EUNATCH] |
The following messages may be sent from this function:
CPE3418 E | Possible APAR condition or hardware failure. |
CPFA0D4 E | File system error occurred. Error number &1. |
CPF3CF2 E | Error(s) occurred during running of &1 API. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
This function is not supported for save files and will fail with error code [ENOTSUP].
See Code disclaimer information for information pertaining to code examples.
The following example uses ftruncate():
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #define string_len 1000 main() { char *mega_string; int file_descriptor; int ret; char fn[]="write.file"; struct stat st; if ((mega_string = (char*) malloc(string_len)) == NULL) perror("malloc() error"); else if ((file_descriptor = creat(fn, S_IWUSR)) < 0) perror("creat() error"); else { memset(mega_string, '0', string_len); if ((ret = write(file_descriptor, mega_string, string_len)) == -1) perror("write() error"); else { printf("write() wrote %d bytes\n", ret); fstat(file_descriptor, &st); printf("the file has %ld bytes\n", (long) st.st_size); if (ftruncate(file_descriptor, 1) != 0) perror("ftruncate() error"); else { fstat(file_descriptor, &st); printf("the file has %ld bytes\n", (long) st.st_size); } } close(file_descriptor); unlink(fn); } free(mega_string); }
Output:
write() wrote 1000 bytes the file has 1000 bytes the file has 1 bytes
Top | UNIX-Type APIs | APIs by category |