basic.%read

C Function/BASIC Program, Releases: AP/Unix

Reads from a Unix file.

Syntax

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

Description

reads the number of bytes designated in 'size' into the 'variable', from the file specified by 'file.descriptor' returned by a previous call to '%open()', '%creat()' or 'dup()'.

A string of a size at least equal to 'size' must have been assigned to 'variable' before the call by either an explicit assignment (eg 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 less than 'size' bytes are returned, the content of the string beyond is undefined, as is usual in C. No data translation occurs. If the data read in variable contains Segment Marks (x'ff'), results are unpredictable. Use '%malloc()' to obtain a Unix memory in which to read binary data.

Example

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

See Also

Command Name Type Description
basic.%malloc C Function Allocates memory.
basic.%rdhex C Function Reads a Unix file and translates to hexadecimal.
basic.cfunction Definition Declare a list of C functions.
basic.cfunc Definition can be called from a Pick/BASIC program or subroutine in AP/DOS and AP/Unix implementations using a syntax similar to that of normal C.
basic.%dup C Function Returns new file descriptor.
basic.%creat C Function Creates new Unix file, or rewrites to existing file.
basic.%ttyname C Function Returns a pointer to a terminal name.
basic.%open C Function Opens a Unix file for reading or writing.
basic.%bind C Function Binds a name to a socket.
basic.%listen C Function Listens for incoming connections and limits the backlog of incoming connections.
basic.%lseek C Function Sets Unix file pointer.

User Comments

What do you think?

Share your experience or ask a question by using the form below.

Login to leave your comments.