fread

Reads data from a stream.

Library:LibC
Classification:ANSI
Service:File and Directory I/O

Syntax

  #include <stdio.h> 
   
  size_t fread (
     void    *buf,
     size_t   elsize,
     size_t   nelem,
     FILE    *fp);
  

Parameters

buf

(OUT) Points to the location of the array to receive data.

elsize

(IN) Specifies the size (in bytes) of each element.

nelem

(IN) Specifies the number of elements.

fp

(IN) Points to the file to be read.

Return Values

If successful, returns the number of complete elements successfully read. This value can be less than the requested number of elements if a read error or end-of-file is encountered.

If an error occurs, fread sets the error indicator for the stream and sets errno to one of the following:

Decimal

Constant

Description

4

EBADF

The file descriptor is not a valid file descriptor open for reading.

5

ENOMEM

Insufficient storage space is available.

24

EAGAIN

The O_NONBLOCK flag is set for the file descriptor, and the process cannot immediately perform the read operation.

25

ENXIO

A request was made of a nonexistent device, or the request was outside the capabilities of the device.

28

EIO

A physical I/O error occurred.

63

EINTR

A signal interrupted the read operation.

81

EOVERFLOW

An attempt was made to read at or beyond the offset maximum associated with the corresponding stream.

Remarks

The fread function reads nelem elements of elsize bytes each from the file specified by fp and places them in the array pointed to by buf. The file position indicator for the file is advanced by the number of bytes successfully read.

See Also