write
Writes data (blocks even if writing to the screen)
#include <unistd.h>
LONG write (
int handle,
char *buffer,
LONG len);
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:
If write does not complete successfully, NetWareErrno is set.
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.
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.
close, creat, dup, dup2, eof, filelength, fileno, fstat, isatty, lseek, open, read, sopen, tell
#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 ();
}