write

Writes data (blocks even if writing to the screen)

Local Servers:blocking
Remote Servers:blocking
Classification:POSIX
Platform:NLM
Service:Operating System I/O

Syntax

  #include <unistd.h>  
   
  LONG write  (  
     int     handle,   
     char   *buffer,   
     LONG    len);
  

Parameters

handle
(IN) Specifies a file handle.
buffer
(IN) Point to the address at which to start transmitting data.
len
(IN) Specifies the number of bytes transmitted.

Return Values

write returns the number of bytes of data transmitted to the file. When there is no error, this is the number given by the len parameter. In the case of an error, such as there being no space available to contain the file data, the return value is less than the number of bytes transmitted. A value of -1 is returned in the case of output errors.

If an error occurs, errno can be set to:

4

EBADF

Bad file number.

If write does not complete successfully, NetWareErrno is set.

Remarks

This function also works on the DOS partition.

The handle value is returned by open, sopen, or creat. The access mode must have included either O_WRONLY or O_RDWR when open or sopen was invoked. The data is written to the file at the end when the file was opened with O_APPEND included as part of the access mode; otherwise, it is written at the current file position for the file in question. This file position can be determined with tell and can be set with lseek.

For Stream files, the operation of write is determined by the values of the minimum and maximum n-byte range (packet size) accepted by the Stream. These values are contained in the topmost Stream module. Unless the user pushes the topmost module, these values cannot be set or tested from user level.

  • If n-byte falls within the packet size range, n-byte bytes are written.
  • If n-byte does not fall within the range and the minimum packet size value is zero, write breaks the buffer into maximum packet size segments prior to sending the data downstream (the last segment can contain less than the maximum packet size).
  • If nbyte does not fall within the range and the minimum value is nonzero, write fails with errno set to ERANGE. Writing a zero-length buffer (n-byte is 0) sends zero bytes with zero bytes returned.

For Stream files, if O_NDELAY is not set and the Stream cannot accept data (the stream write queue is full due to internal flow control conditions), write blocks until data can be accepted. O_NDELAY prevents a process from blocking due to flow control conditions. If O_NDELAY is set and the Stream cannot accept data, write fails. If O_NDELAY is set and part of the buffer has been written when a condition in which the Stream cannot accept additional data occurs, write terminates and returns the number of bytes written.

See Also

close, creat, dup, dup2, eof, filelength, fileno, fstat, isatty, lseek, open, read, sopen, tell

Example

  #include   <stddef.h>  
  #include   <unistd.h>  
  #include   <fcntl.h>  
  #include   <errno.h>  
  #include   <string.h> 
    
  main()  
  {  
     int fh,size;  
     char buffer[] = { "NLM test file" };  
     size = strlen(buffer);  
     errno = 0;  
     if((fh = creat ("test.dt1",0)) != -1)  
     {  
        printf ("open 1:  %s\r\n",strerror(errno));  
     }  
     else  
     {  
        if(write (fh,buffer,size) < size)  
        printf ("write 1:  %s\r\n",strerror(errno));  
        printf ("file length 1: %d\r\n",filelength(fh));  
        if(close (fh) < 0)  
        printf ("close 1:  %s\r\n",strerror(errno));  
     }  
     if((fh = creat ("test.dt2",0)) != -1)  
     {  
        printf ("open 2:  %s\r\n",strerror(errno));  
     }  
     else  
     {  
        if(write (fh,buffer,size) < size)  
        printf ("write 2:  %s\r\n",strerror(errno));  
        printf ("file length 2: %d\r\n",filelength(fh));  
        if(close (fh) < 0)  
        printf ("close 2:  %s\r\n",strerror(errno));  
     }  
     if((fh = creat ("test.dt3",0)) != -1)  
     {  
        printf ("open 3:  %s\r\n",strerror(errno));  
     }  
     else  
     {  
        if(write (fh,buffer,size) < size)  
        printf ("write 3:  %s\r\n",strerror(errno));  
        printf ("file length 3: %d\r\n",filelength(fh));  
        if(close (fh) < 0)  
        printf ("close 3:  %s\r\n",strerror(errno));  
     }  
     printf ("\r\n exit:  %s\r\n",strerror(errno));  
     getch ();  
  }