%read() function

The %read() function reads the number of bytes designated in size into var, from the file specified by file.descriptor. This file is returned by a previous call to %open(), %creat(), or %dup().

Note: When using this function with sockets: For UNIX: Use the socket descriptor that results from the %accept() function call. For Windows: Use the %recv() function.

Syntax

n = %read(file.descriptor, var, size)

Description

A string of a size at least equal to size must have been assigned to var before the call by either an explicit assignment (for example, buffer=space(1000)) or by the char reserve statement, or else the data is truncated.

This function returns the number of bytes actually read. If the bytes returned are less than size, the content of the trailing string is undefined, as is normal in C. No data translation occurs. If the data read in var contains segment marks (x’ff’), results are unpredictable. Use %malloc() to obtain a memory block in which to read binary data.

Example(s)

item=’’
char buffer[10000] ;* reserve buffer space
loop while true do
   n=%read(fd, buffer, 10000)
   if n=-1 then
      print "Error ":system(0)
      stop
   end else
      if n<10000 then
         * The whole file has been read.
         item=item:buffer[1,n]
         convert char(10) to char(254) in item
         write item on itemname
         stop
         end else
      * Some more to read
      item=item:buffer
   end
end
repeat