previous | start | next

The write Function

The write Function:

      write(fd, addr, N)
     

The write function returns the number bytes written, but this is mostly ignored since it is equal to N.

    1   #include <fcntl.h> // for open, read, write, and close
    2   int main()
    3   {
    4     int fd;
    5     const int N = 8;
    6     char buf[N];
    7     size_t nrd;
    8   
    9     fd = open("foo.txt", O_RDONLY);
   10     if ( fd < 0 ) {
   11       printf("Could not open file foo.txt for input.\n");
   12       exit(1);
   13     }
   14   
   15     while( (nrd = read(fd, buf, N)) > 0 ) {
   16       write(1, buf, N);
   17     }
   18     return 0;
   19   }


previous | start | next