fread

Reads data from a stream

Local Servers:blocking
Remote Servers:blocking
Classification:ANSI
Platform:NLM
Service:Stream 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 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

The fread function returns the number of complete elements successfully read. This value can be less than the requested number of elements.

Call feof and ferror to determine whether the end of the file was encountered, or if an input/output error has occurred. If an error occurs, errno is set.

Remarks

This function also works on the DOS partition.

The fread function reads nelem elements of elsize bytes each from the file specified by fp.

See Also

feof, ferror, fopen, read

fread Example

The following example reads a simple student record containing binary data. The student record is described by the struct student_data declaration.

  #include <stdio.h>  
   
  struct student_data  
  {  
     int student_id;  
     unsigned char marks [10];  
  };  
   
  int   read_data (FILE *fp, struct student_data *p)  
  {  
     return (fread (p, sizeof (*p), 1, fp) );  
  }