write()
Write to a PSL process or file channel.
Syntax
Parameter
Parameter | Definition |
---|---|
chan | process I/O channel number to which text is written |
text | text to be written to channel chan The text can be a text string enclosed in double quotation marks, or one or more PSL commands that produce text as output. |
Description
The write() function writes text to channel chan. The write() function returns the number of characters written or -1 on error.
If text cannot be written immediately, the write() function call blocks until it can either write the whole of text or the channel terminates.
To enforce serialization for shared channels, no two reader processes (that is, the read() or readln() functions) can be blocked on the same channel. The second reader process that attempts to block on the shared channel will fail, returning the NULL string and setting the PSL variable errno to E_PSL_BUSY_CHANNEL.
Another possible shared channel failure can be caused by a close() function being executed against a channel that also has a blocked reader process. The close() function will cause the reader process to return the NULL string and set errno to E_PSL_UNBLOCKED_BY_CLOSE.
Example
The following PSL script uses the write() function to write to a disk file the table of decimal/octal/hexadecimal conversions that was created using the sprintf() function:
local channel,string;
i = 0;
channel = fopen("/local/convert.txt","w");
if (channel != "") {
string = sprintf(" n Oct Hex\n");
string = sprintf("%s--- ---- ----\n",string);
while (i++ <= 256) {
string = sprintf("%s%3d %4o %4X\n",string,i,i,i);
}
write(channel,string);
close(channel);
} else {
printf("File could not be opened, errno = %d",errno);
}
return;
}