Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

72 File I/O Chapter 3


3.8 writeFunction


Data is written to an open file with thewritefunction.
#include <unistd.h>
ssize_t write(intfd,const void *buf,size_t nbytes);
Returns: number of bytes written if OK,−1 on error

The return value is usually equal to thenbytes argument; otherwise, an error has
occurred. A common cause for awriteerror is either filling up a disk or exceeding the
file size limit for a given process (Section 7.11and Exercise 10.11).
For a regular file, the write operation starts at the file’s current offset. If the
O_APPENDoption was specified when the file was opened, the file’s offset is set to the
current end of file beforeeach write operation. After a successful write, the file’s offset
is incremented by the number of bytes actually written.

3.9 I/O Efficiency


The program in Figure3.5 copies a file, using only thereadandwritefunctions.
#include "apue.h"
#define BUFFSIZE 4096
int
main(void)
{
int n;
char buf[BUFFSIZE];
while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
if (write(STDOUT_FILENO, buf, n) != n)
err_sys("write error");
if (n < 0)
err_sys("read error");
exit(0);
}

Figure 3.5Copy standardinput to standardoutput

The following caveats apply to this program.
•Itreads from standardinput and writes to standardoutput, assuming that these
have been set up by the shell beforethis program is executed. Indeed, all
normal UNIX system shells provide a way to open a file for reading on standard
input and to create (or rewrite) a file on standardoutput. This prevents the
program from having to open the input and output files, and allows the user to
take advantage of the shell’s I/O redirection facilities.
Free download pdf