gtpc2m9o | C/C++ Language Support User's Guide |
This function writes data to a file.
Note |
---|
This description applies only to files. See the TPF Transmission Control Protocol/Internet Protocol for more information about the write function for sockets. |
TPF deviation from POSIX |
---|
The TPF system does not support the ctime (change time) time stamp. |
Format
#include <unistd.h> ssize_t write(int fildes, const void *buf, size_t N);
This function writes N bytes from buf to the file associated with fildes. N must not be greater than LONG_MAX (defined in the limits.h header file). If N is zero, the write function returns a value of zero without attempting any other action.
If fildes refers to a regular file or any other type of file on which a process can seek, the write function begins writing at the file offset associated with fildes. A successful write function increments the file offset by the number of bytes written. If the incremented file offset is greater than the previous length of the file, the length of the file is set to the new file offset.
If fildes refers to a file on which a process cannot seek, the write function begins writing at the current position. There is no file offset associated with such a file.
If O_APPEND (defined in the fcntl.h header file) is set for the file, the write function sets the file offset to the end of the file before writing the output.
If there is not enough room to write the requested number of bytes (for example, because there is not enough room on the disk), the write function writes as many bytes as the remaining space can hold.
For character special files that support nonblocking writes and cannot accept data immediately, the effect is one of the following:
A successful write function updates the modification time for the file.
Normal Return
If successful, the write function returns the number of bytes written, less than or equal to N.
Error Return
If unsuccessful, the write function returns a -1 value and sets the errno function to one of the following:
Programming Considerations
The TPF system does not support creating, updating, or deleting files in 1052 or UTIL state. Special files may or may not be writable in 1052 or UTIL state depending on the device driver implementation.
Examples
The following example writes 1 000 000 bytes to a file.
#include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <stdio.h> #define mega_string_len 1000000 main() { char *mega_string; int fd, ret; char fn[]="write.file"; if ((mega_string = (char*) malloc(mega_string_len)) == NULL) perror("malloc() error"); else if ((fd = creat(fn, S_IWUSR)) < 0) perror("creat() error"); else { memset(mega_string, '0', mega_string_len); if ((ret = write(fd, mega_string, mega_string_len)) == -1) perror("write() error"); else printf("write() wrote %d bytes\n", ret); close(fd); unlink(fn); } }
Output
write() wrote 1000000 bytes
Related Information
See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.