Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

read - Read from a file (elementary)

&pagelevel(4)&pagelevel

Definition

#include <stdio.h>

int read(int fd, char *puf, int n);

read is the elementary read function.

read reads from the file with file descriptor fd a maximum of n characters into the area
pointed to by buf.

In text files, read only reads the characters within one line per call. Input is terminated at
the end of the line.
In binary files, newline (\n) characters are ignored by read.
SAM files are always processed as text files with elementary functions.

Parameters  int fd

File descriptor for the input file.
A file descriptor (positive integer) is the result of a successful open/open64 or
creat/creat64 call.
The file descriptors for stdin (0), stdout (1), and stderr (2) are automatically
assigned when the program is started.

char *buf

Pointer to the area into which the read data is to be written. The area should be at least
n bytes in size.

int n

Maximum number of bytes to be read. If the end of the line is reached first, fewer than
n bytes will be read.

Return val.

The number of bytes actually read

if successful.

0

-1

for end of file.

if nothing was read due to one of the following errors:

  • physical I/O error

  • fd is not a valid file descriptor

  • the file is not present

  • no access permission for the file exists

  • n is impossible

Notes

The number of bytes actually read may be less than the specification in n if the end of the
line is reached first (only applies to text files) or if end of file or an error is encountered.

You should use sizeof to ensure that the number of bytes read does not exceed the
amount that can be accepted by the buffer.

The following applies in the case of text files with SAM access mode and variable record
length for which a maximum record length is also specified: When the specification O_NO-
SPLIT was entered for open, records of maximum record length are not concatenated with
the subsequent record when they are read. By default (i.e. without the specification O_NO-
SPLIT), when a record with maximum record length is read, it is assumed that the following
record is the continuation of this record and the records are concatenated.

In the case of files with record-oriented input/output (record I/O), i.e. when the specification
O_RECORD was entered for open, the read function reads a record (or block) from the
current file position. If the number n of the characters to be read is greater than the current
record length, nevertheless only this record is read. If n is less than the current record
length, only the first n characters are read. The data of the next record is read when the next
read access takes place.

Example

The following program copies the standard input (file descriptor 0) to the standard output
(file descriptor 1). If you use the redirection mechanism for stdin and stdout
(PARAMETER-PROMPTING in the RUNTIME option), you can copy from any source to
any destination with this program. BUFSIZ (8192 bytes) is defined in the include file
<stdio.h>.

#include <stdio.h>
int main(void)
{
  char buf[BUFSIZ];
  int n;
  while((n = read(0, buf, sizeof(buf))) > 0)
        write(1, buf, n);
  return 0;
}

See also

write, open, open64, creat, creat64