Read (system call)
   HOME

TheInfoList



OR:

In modern
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming inter ...
compliant operating systems, a program that needs to access data from a
file File or filing may refer to: Mechanical tools and processes * File (tool), a tool used to ''remove'' fine amounts of material from a workpiece **Filing (metalworking), a material removal process in manufacturing ** Nail file, a tool used to gent ...
stored in a file system uses the read system call. The file is identified by a file descriptor that is normally obtained from a previous call to
open Open or OPEN may refer to: Music * Open (band), Australian pop/rock band * The Open (band), English indie rock band * ''Open'' (Blues Image album), 1969 * ''Open'' (Gotthard album), 1999 * ''Open'' (Cowboy Junkies album), 2001 * ''Open'' ( ...
. This system call reads in data in
byte The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable uni ...
s, the number of which is specified by the caller, from the file and stores then into a buffer supplied by the calling process. The read system call takes three arguments: # The file descriptor of the file. # the buffer where the read data is to be stored and # the number of bytes to be read from the file.


POSIX usage

The read system call interface is standardized by the POSIX specification. Data from a file is read by calling the read function: ssize_t read(int fd, void *buf, size_t count); The value returned is the number of bytes read (zero indicates
end of file In computing, end-of-file (EOF) is a condition in a computer operating system where no more data can be read from a data source. The data source is usually called a file or stream. Details In the C standard library, the character reading funct ...
) and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a
pipe Pipe(s), PIPE(S) or piping may refer to: Objects * Pipe (fluid conveyance), a hollow cylinder following certain dimension rules ** Piping, the use of pipes in industry * Smoking pipe ** Tobacco pipe * Half-pipe and quarter pipe, semi-circular ...
, or from a
terminal Terminal may refer to: Computing Hardware * Terminal (electronics), a device for joining electrical circuits together * Terminal (telecommunication), a device communicating over a line * Computer terminal, a set of primary input and output dev ...
), or because the system call was interrupted by a
signal In signal processing, a signal is a function that conveys information about a phenomenon. Any quantity that can vary over space or time can be used as a signal to share messages between observers. The '' IEEE Transactions on Signal Processing' ...
. Alternatively, -1 is returned when an error occurs, in such a case
errno errno.h is a header file in the standard library of the C programming language. It defines macros for reporting and retrieving error conditions using the symbol errno (short for "error number").International Standard for Programming Language C ( ...
is set appropriately and further it is left unspecified whether the file position (if any) changes.


See also

* write (system call)


References


POSIX read


External links

* {{Computer files C POSIX library System calls